C中的双指针

发布于 2024-12-04 20:02:26 字数 256 浏览 1 评论 0原文

有人请详细说明这里发生了什么吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凉城已无爱 2024-12-11 20:02:26

首先,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 of p is 4.

鹤仙姿 2024-12-11 20:02:26

p 是一个指向int 指针的指针。它被初始化为0,即它是一个空指针。

然后它会递增以指向内存中下一个连续的指向 int 的指针。* 下一个指针将位于地址 4,因为在您的平台上,a 的大小指针为4字节。

然后 printf 将指针值解释为整数,因此显示“4”。

* 但请注意,这现在是未定义的行为。

p is a pointer to a pointer-to-int. It's being initialised to 0, 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.

毁我热情 2024-12-11 20:02:26

很明显。您有一个指向 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 incrementing p 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! :)

鹤仙姿 2024-12-11 20:02:26

++p 实际上是未定义的行为,但在您的实现中似乎发生的是 sizeof(int*) 为 4,并且空指针是地址 0。回想一下该指针增量(当它不是 UB 时)会向地址添加等于引用对象类型大小的字节数。因此,当您采用 int** 类型的空指针(因此引用类型为 int*)并递增它时,您最终会得到地址4。只是无法保证。

%d 格式需要 int 时传递指针也是未定义的行为,但似乎 intint 的表示** 足够兼容,并且您的实现上的 varargs 调用约定对它们的处理非常相似,因此它已成功打印 4。对于 sizeof(int) == sizeof(int**) 的实现来说,这也并不奇怪,但也不能保证。

当然,由于这是未定义的行为,因此您所看到的还有其他可能的解释。

++p is actually undefined behaviour, but what appears to have happened on your implementation is that sizeof(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 type int** (hence the referand type is int*) and increment it, you end up at the address 4. It's just not guaranteed.

Passing a pointer when the %d format expects an int is also undefined behavior, but it appears that the representation of int and int** are sufficiently compatible, and the varargs calling convention on your implementation treats them sufficiently similarly, that it has successfully printed 4. That's also not very surprising for implementations where sizeof(int) == sizeof(int**), but also isn't guaranteed.

Of course since it's undefined behavior, there are other possible explanations for what you see.

2024-12-11 20:02:26

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文