繁体   English   中英

持有数组地址的指针地址如何相同?

[英]How the address of pointer that holds the address of array are same?

这里p是一个整数指针,可以保存int变量的地址,但它也有一个内存地址-将其存储在其中。

设数组a = 1002基地址a = 1002指针p = 2008 a = 1002地址p = 2008

当我们写: int *p=a; //p points to the base address of array a int *p=a; //p points to the base address of array a
int **r=&p; //means *r points to the address of p int **r=&p; //means *r points to the address of p

*r如何指向a的地址,它应该指向p地址。

#include <stdio.h>
void main()
{
    int a[3] = {1, 2, 3};
    int *p =a;
    int **r = &p;
    printf("%p %p", *r, a);
}

您的printf不正确。 打印r指向的地址应该是r

printf("%p %p", r, a);

通过使用*r ,你顺从r即跳转到地址r指向),因此打印的地址a

请注意

int *p=a;

表示ap现在指向相同的地址。

而且, r指向p (是的地址p ),而不是*r

因此,要打印p的地址,只需在打印*r printf()使用r而不是*r

printf("%p %p", r, a);

下面是什么?

 int **r = &p;

基本上使用上面的, r拥有p地址p吗? 因此,如果您像对*r一样取消对r引用,它将尝试检索存储在p地址中的值, p吗? 这是a

注意:您需要在printf中强制转换为void*

 printf("%p %p", (void*)*r, (void*) a);

不是->->与->相同

当你说,

int *p = a;

这意味着P指向a,或者P持有a的地址。

int **r=&p;情况下相同

如果您使用了printf("%p %p", r, a); R保存P的地址printf("%p %p", r, a); ,您将获得P的地址。

但是由于您取消引用r,因此获得了a的地址。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM