为什么大小对于不同的指针是不同的 [英] Why Size is different for different pointers
本文介绍了为什么大小对于不同的指针是不同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
#define R 10
#define C 20
int main()
{
int *p;
int *p1[R];
int *p2[R][C];
printf("%d %d %d", sizeof(*p),sizeof(*p1),sizeof(*p2));
getchar();
return 0;
}
为什么输出:4 8 160
?为什么p1
的大小变成8而不是4?
Why is the output: 4 8 160
? Why does size of p1
becomes 8 and not 4?
推荐答案
考虑类型.
-
sizeof(*p)
==>sizeof(int)
-
sizeof(*p1)
==>sizeof(int *)
-
sizeof(*p2)
==>sizeof((int [20]))
sizeof(*p)
==>sizeof(int)
sizeof(*p1)
==>sizeof(int *)
sizeof(*p2)
==>sizeof((int [20]))
注意:根据您的平台和编译器,您将获得不同的结果.
Note: Depending on your platform and compiler, you'll get different results.
另外,我们知道,sizeof
会生成类型为size_t
的结果,建议使用%zu
格式说明符来打印结果.
Also, as we know, sizeof
produces a result of type size_t
, it's advised to use %zu
format specifier to print the result.
这篇关于为什么大小对于不同的指针是不同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文