[英]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;
表示a
和p
现在指向相同的地址。
而且, 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.