2

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
and 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);
}
4
  • 2
    Why wouldn't it be the same ? You let r point to the address where p is stored and then print *r, so obviously that is p, which you assigned to be a Commented Jul 28, 2015 at 11:56
  • It should be int *p = &a; or else it is an invalid conversion Commented Jul 28, 2015 at 11:59
  • 1
    @DevangJayachandran: int [3] decays to int* Commented Jul 28, 2015 at 12:06
  • Oh right sorry! My bad. Commented Jul 28, 2015 at 12:08

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

2

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);

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.