我试图从函数返回一个动态声明的数组; 到目前为止,我返回一个结构来保存指向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)