Ç - 中SizeOf指针 [英] C - SizeOf Pointers

查看:164
本文介绍了Ç - 中SizeOf指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char c[] = {'a','b','c'};
int* p = &c[0];
printf("%i\n", sizeof(*p)); //Prints out 4
printf("%i\n", sizeof(*c)); //Prints out 1

我约code的这一部分极其混乱。 P和c重新present的数组c的第0个索引处的地址。但为什么的sizeof(* P)打印出4?难道不应该是1?

I am extremely confused about this section of code. Both p and c represent the address of the array c at the 0th index. But why does sizeof(*p) print out 4? Shouldn't it be 1?

推荐答案

由于 P 的类型为为int * ,所以 * p 的类型 INT ,这是明显宽4字节您的实现的。

Because p is of type int *, so *p is of type int, which is apparently 4 bytes wide on your implementation.

和使用%祖打印为size_t (什么的sizeof 收益率),如果你不想让你的程序来调用未定义的行为。

And use %zu for printing size_t (what sizeof yields) if you don't want your program to invoke undefined behavior.

这篇关于Ç - 中SizeOf指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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