简体   繁体   中英

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

Here p is a integer pointer that can hold the address of int variable, but it also has a memory address - where it is stored.

let base address of array a = 1002 address of pointer p = 2008

when we write: int *p=a; //p points to the base address of array a int *p=a; //p points to the base address of array a
and int **r=&p; //means *r points to the address of p int **r=&p; //means *r points to the address of p

how *r points to the address of a , it should point to address of p .

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

Your printf in not correct. It should be r to print the address r points to:

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

By using *r , you deference r (ie, jump to the address r is pointing to) and thus printing the address of a .

Please note that

int *p=a;

means that a and p are now pointing to same address.

Also, it is r that points to p (is the address of p ) and not *r .

Thus to print the address of p simply use r instead of *r in printf() .

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

What is below?

 int **r = &p;

Basically using above, r holds address of p right? So if you dereference r , like you do *r , it will try to retrieve value stored at address of p right? Which is a .

Note: You need to cast to void* in printf:

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

Isn't ->-> same as ->

When you say,

int *p = a;

it means, P is pointing to a, or P holds the address of a.

Same in case of int **r=&p;

R holds the address of P, had you used printf("%p %p", r, a); , you would have got the address of P.

but since you are dereferencing r, you got the address of a.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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