int *p 应该改为 long int *p 吗?
我通过阅读 K&R 并做练习来学习 c。我现在正在阅读第 5 章,其中涉及指针。我不明白为什么这个语句:
int *p;
不是:
long int *p;
因为 *p 包含一个地址,并且不能保证 int 类型的变量足够大以容纳一个大地址。或者有吗?
I'm learning c by reading K&R and doing the exercises. I'm now in chapter 5 which deals with pointers. I don't understand why the statement:
int *p;
is not:
long int *p;
since *p contains an address and there is no guarantee that a variable of type int will be large enough to hold a large address. Or is there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
int 是指针指向的对象的类型,而不是指针本身的大小。指针的大小与其指向的对象无关
例如:
指针 p 和 d “通常”具有相同的大小,但它们指向的数据不具有相同的大小尺寸。
编辑:正如评论中所指出的,指针实际上并不“要求”具有相同的大小。
正如约翰所解释的:
int is the type of the object that the pointer points to and not the size of the pointer itself. The size of the pointer is independant of the object it points to
For example:
Both
pointers
p and d would "normally" have the same size, but the data that they point to don't have the same size.Edit: As pointed out in the comments pointers aren't actually "required" to be of the same size.
As John explained:
long int *
表示指向long int
的点。这并不意味着长指针。
指针是独立的东西,并且有自己的大小。 (取决于您要编译的位数)
long int *
means a point to along int
.It doesn't mean a long pointer.
Pointers are separate things and will have their own size. (dependent on the bitness that you're compiling for)
int *
不是int
类型。它是一个指针类型。因此它足够大以存储任何地址。long int *
与int *
的大小相同——只是您将所指向的内容视为不同的。int *
is not of typeint
. It is a pointer type. Therefore it will be large enough to store any address.long int *
is the same size asint *
-- it's just that you're treating where things are pointed to as different.这是一个指向 int 的指针(我倾向于写为“int* p”)。这基本上意味着 p 是一个指向 int 的指针。
第二个是指向 long int 的指针。
它们都是指针,因此存储字节数相同,但一个引用 int,另一个引用 long int。
This is a pointer to an int (I tend to write as 'int* p'). This basically means that p is a pointer to an int.
The second is a pointer to a long int.
They are both pointers, and therefore the same number of bytes of storage, but one references an int, the other a long int.
不要将指针的类型与其所指向的类型混淆。 表达式
*p
的类型是int
,但p
的类型是int *< /代码>。
Don't confuse the type of the pointer with the type of what it points to. The type of the expression
*p
isint
, but the type ofp
isint *
.