小编car*_*l13的帖子

计算机(C编译器或其他东西)如何处理"自动数组声明"?| C语言

我试图从函数返回一个动态声明的数组; 到目前为止,我返回一个结构来保存指向malloc()为数组分配的内存块的指针一个整数来存储数组的长度.


这让我很奇怪; C编译器(或其他)如何处理程序中声明的自动数组? 例如.

main()
{
    //delcare an array holding 3 elements
    int array[] = {1,2,3};


    /*variable to hold length of array
     *size of array / size of 1st element in the array == length of the array
     *this will == 3
     */ 
    int array_Length = (sizeof(array))/(sizeof(*array));


    //call malloc for a block of memory to hold 3 integers(worth of memory)
    int* ptr = malloc(3*(sizeof(int)));


    /*not exactly sure what this formula means when using …
Run Code Online (Sandbox Code Playgroud)

c arrays memory-management sizeof dynamic-arrays

2
推荐指数
1
解决办法
154
查看次数

标签 统计

arrays ×1

c ×1

dynamic-arrays ×1

memory-management ×1

sizeof ×1