C 语言中 sizeof 运算符采用什么参数? [英] What arguments does the sizeof operator take in C?

查看:32
本文介绍了C 语言中 sizeof 运算符采用什么参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[原标题是指'sizeof function'.]

我尝试了这些,它们都有效:

<前>字符 *p;printf("*p 的大小为 %d\n",sizeof(*p));//结果=1printf("p 的大小为 %d\n",sizeof( p));//结果=4printf("p 的大小为 %d\n",sizeof(&p));//结果=4

我想知道为什么第一个 printf 是 1,第二个和第三个是 4?那么 sizeof 实际上可以接受哪些参数?

解决方案

需要一个类型.

sizeof(char) 始终为 1.变量 p 本身是一个指针,在你的平台上它的大小为 4.然后你做 &p,或者一个指向指针的指针,它也有大小为 4.

在大多数现代桌面系统上,32 位架构将有 4 个字节的指针,而 64 位架构将有 8 个字节的指针.

sizeof 本身是一个关键字,在编译时解析,而不是一个函数.在 C99 中,数组可以是可变长度的,sizeof 会等到运行时解析这个大小.

[Original title referred to 'sizeof function'.]

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

I wonder why the first printf is 1, the 2nd and 3rd is 4? So what arguments can sizeof can actually take?

解决方案

It takes a type.

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.

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 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.

这篇关于C 语言中 sizeof 运算符采用什么参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆