C中的双指针
有人请详细说明这里发生了什么吗?
int main()
{
int **p = 0;
//p=? and why| *p=? and why|**p=? and why
++p;
//p=? and why| *p=? and why|**p=? and why
printf("%d\n", p);
return 1;
}
输出:-
- 4(为什么?)
Anyone please elaborate what is happining here?
int main()
{
int **p = 0;
//p=? and why| *p=? and why|**p=? and why
++p;
//p=? and why| *p=? and why|**p=? and why
printf("%d\n", p);
return 1;
}
output:-
- 4 (why?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,
p
是一个指向整数的指针。int **p = 0;
p
= 0,*p
= 无,**p
= 小于没有什么。++p;
与 p = p + 1 相同。进一步表示指向 int 指针的一个指针的大小。至少在您的操作系统上,指针基本上是 32 位长度(4 个字节)。
p
现在指向 0 之后的 4 个字节。p
的值为 4。First of all,
p
is a pointer to a pointer-to-integer.int **p = 0;
p
= 0,*p
= nothing,**p
= less than nothing.++p;
Same as p = p + 1. Means the size of one pointer to a pointer-to-int further. A pointer is basically, at least on your OS, 32 bits length (4 bytes).
p
now points 4 bytes after 0. The value ofp
is 4.p
是一个指向int
指针的指针。它被初始化为0
,即它是一个空指针。然后它会递增以指向内存中下一个连续的指向
int
的指针。* 下一个指针将位于地址 4,因为在您的平台上,a 的大小指针为4字节。然后 printf 将指针值解释为整数,因此显示“4”。
* 但请注意,这现在是未定义的行为。
p
is a pointer to a pointer-to-int
. It's being initialised to0
, i.e. it's a null pointer.It's then being incremented to point at the next consecutive pointer-to-
int
in memory.* The next pointer will be at address 4, because on your platform the size of a pointer is 4 bytes.Then printf interprets the pointer value as an integer, and so displays "4".
* Note, however, that this is now undefined behaviour.
很明显。您有一个指向 int 的指针(
int **p
表示指向 int 的指针),它实际上保存地址 0)。在您的体系结构中,指针本身的长度为 32 位(4 字节),因此递增p
会得到 p+4,即 0+4 = 4。去读一本不错的 C 书,然后了解指针算术。你会幸福一辈子的! :)
It is clear. You have a pointer to a pointer to int (
int **p
means a pointer to a pointer to int), that actually holds the address 0). A pointer in itself, in your architecture, is 32 bits (4 bytes) long, so incrementingp
gives you p+4, that is, 0+4 = 4.Go get a nice C book and learn about pointer arithmetic. You'll be glad the rest of your life! :)
++p
实际上是未定义的行为,但在您的实现中似乎发生的是sizeof(int*)
为 4,并且空指针是地址 0。回想一下该指针增量(当它不是 UB 时)会向地址添加等于引用对象类型大小的字节数。因此,当您采用int**
类型的空指针(因此引用类型为int*
)并递增它时,您最终会得到地址4
。只是无法保证。当
%d
格式需要int
时传递指针也是未定义的行为,但似乎int
和int 的表示**
足够兼容,并且您的实现上的 varargs 调用约定对它们的处理非常相似,因此它已成功打印4
。对于 sizeof(int) == sizeof(int**) 的实现来说,这也并不奇怪,但也不能保证。当然,由于这是未定义的行为,因此您所看到的还有其他可能的解释。
++p
is actually undefined behaviour, but what appears to have happened on your implementation is thatsizeof(int*)
is 4, and a null pointer is address 0. Recall that pointer increment, when it's not UB, adds a number of bytes to the address equal to the size of the referand type. So it's not all that surprising that when you take a null pointer of typeint**
(hence the referand type isint*
) and increment it, you end up at the address4
. It's just not guaranteed.Passing a pointer when the
%d
format expects anint
is also undefined behavior, but it appears that the representation ofint
andint**
are sufficiently compatible, and the varargs calling convention on your implementation treats them sufficiently similarly, that it has successfully printed4
. That's also not very surprising for implementations wheresizeof(int) == sizeof(int**)
, but also isn't guaranteed.Of course since it's undefined behavior, there are other possible explanations for what you see.
p 是一个指向 int 的指针。并且它被初始化为0,即NULL。
当您递增它时,它现在指向指向 int 的下一个指针,在 32 位系统上,该指针恰好是 4。
p is a pointer to pointer to int. And it's initialized to 0, i.e. NULL.
When you increment it, it now points to next pointer to int, which, on 32-bit systems, happens to be 4.