保存数组地址的指针的地址是相同的

How the address of pointer that holds the address of array are same?

本文关键字:地址 指针 数组 组地址 保存      更新时间:2023-10-16

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

让阵列a = 1002的基址指针p = 2008地址

当我们写:int *p=a; //p points to the base address of array a
and 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的地址,只需在printf()中使用r而不是*r

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

下面是什么?

 int **r = &p;

基本上使用以上,r保存p的地址对吗?所以如果你像解引用*r一样解引用r,它会尝试检索存储在地址p的值对吗?即a

注意:你需要在printf:

中转换为void*
 printf("%p %p", (void*)*r, (void*) a);

与->不一样

当你说

int *p = a;

表示p指向a,或者p保存着a的地址

int **r=&p;

R保存p的地址;如果你使用printf("%p %p", r, a);,你会得到p的地址。

但是由于你对r进行了解引用,所以你得到了a的地址