【发布时间】:2020-12-18 08:03:09
【问题描述】:
我需要构建一个函数,该函数将内存分配给一个指针以形成一个数组,但无论我尝试什么,它都不会分配内存。
#include <stdio.h>
#include <stdlib.h>
void alocare(int **a,int n)
{
*a = (int*) malloc(n*sizeof(int));
}
int main()
{
int *a ,*b,*c,n,m,k;
printf("Insert the number of elements for the first array:\n");
scanf("%d",&n);
printf("Insert the number of elements for the second array:\n");
scanf("%d",&m);
printf("Number of bytes ocupied by a before alloc(): %d\n",sizeof(a));
printf("Number of bytes occupied by b before alloc(): %d\n",sizeof(b));
alocare(&a,n);
alocare(&b,m);
printf("Number of bytes ocupied by a after alloc(): %d\n",sizeof(a));
printf("Number of bytes ocupied by b after alloc(): %d\n",sizeof(a));
return 0;
}
它在两个printf中显示的大小在分配之前和之后都是4,我不明白为什么 谢谢!
【问题讨论】:
-
什么是...
sizeof (a_pointer)?
标签: c dynamic-memory-allocation