我正在尝试编写一个可以打印指针变量大小的ac程序,该变量指向malloc()分配的内存位置.
我正在使用的C版本是C99.
以下是我的代码:
#include<stdio.h>
int main()
{
int *temp;
temp=malloc(sizeof(int)*3);
printf("%d %d\n",sizeof(int),sizeof(temp));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
sh-4.2 $ ./f1 4 8
根据代码,printf应该为两个表达式打印相同的值.即12. sizeof(int)= 4.
因为我为temp分配了12个字节的内存,所以sizeof(temp)应该返回12.但是,它返回值8.
我想知道可能是什么原因?如果有人知道,请回答.