问题描述
[原标题提到sizeof的功能。的]
我尝试了这些,他们都工作:
I tried these and they all worked:
char *p;
printf("Size of *p is %d\n",sizeof(*p)); //result =1
printf("Size of p is %d\n",sizeof( p)); //result =4
printf("Size of p is %d\n",sizeof(&p)); //result =4
我不知道为什么第一个printf是1,第2和第3是4?
所以可以sizeof的实际上可以采取什么样的参数?
I wonder why the first printf is 1, the 2nd and 3rd is 4?So what arguments can sizeof can actually take?
推荐答案
这需要一个类型。
的sizeof(char)的
始终为1。变量 P
本身就是一个指针,你的平台上,有一个大小4.然后你做&安培; P
或一个指针的指针,其中也有一个尺寸为4
sizeof(char)
is always one. The variable p
itself is a pointer, and on your platform that has a size of 4. Then you do &p
, or a pointer to a pointer, which also has a size of 4.
在大多数现代桌面系统,32位架构将有4个字节的指针,而64位架构将有8个字节的指针。
On most modern desktop systems, a 32-bit architecture will have 4 byte pointers, while a 64-bit architecture will have 8 byte pointers.
的sizeof
本身是一个关键字,在编译时解决,而不是一个函数。在C99,数组可以是可变长度,和sizeof会等到运行时来解决这个尺寸。
sizeof
itself is a keyword, resolved at compile-time, not a function. In C99, arrays can be variable length, and sizeof will wait until run-time to resolve this size.
这篇关于不sizeof操作符需要用C哪些参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!