简体   繁体   中英

Pointers to pointer

Hello guys can someone explain why while declaring pointers to pointer we need to use ** why cant we use only single * to point a pointer to another pointer or is it just a syntax related issue Eg

int main()
{
    int a=5,*b,*c;
    b=&a;
    c=&b    //Why cant this simply doesn't make c point to memory location of b with the above pointer declaration why is there a need to declare c as **c
}

In this case

int main()
{
    int a=5,*b,*c;
    b=&a;
    c=&b;
}

Here b points to a and c points to b . It is what you have commented in the commented.

c still points to the memory location of b.

The catch is : When you de-reference b ie *b = a = 5 .
But When you de-reference c ie *c = b = &a . So When you dereference c the output would be address of a instead of the value of the variable a

PS : you will face this warning when compiling the code warning: assignment from incompatible pointer type

With the following codes:

int a=5,*b,**c;
b=&a;
c=&b;

We have:

    +---+
a   | 5 |  <-- value
    +---+
    |100|  <-- address
    +---+

    +---+
*b  |100|  <-- value
    +---+
    |200|  <-- address
    +---+

    +---+
**c |200|  <-- value
    +---+
    |300|  <-- address
    +---+

When you store a's address in b, b's value is a's address. But b has it's own address (200). c can store b's address as it's value. But c has it's own address too (300).

printf("%x", &c); will give you: 300

Deferencing *c will get you down "1 level" and give you 100 (get value of address 200)

Deferencing **c will get you down 1 more level and give you 5 (get value of address 100)


If you try to use *c instead of **c to hold *b, how are you able to deference all the way down to reach value 5?

Testing the codes on a compiler:

printf("Address of a: %x\n", &a);
printf("Address of b: %x\n", &b);
printf("Address of c: %x\n", &c);

printf("Value of a: %d\n", a);            
printf("Value of b: %x\n", b);  
printf("Value of c: %x\n", c);  

Output:

Address of a: 28ff44
Address of b: 28ff40
Address of c: 28ff3c
Value of a: 5
Value of b: 28ff44
Value of c: 28ff40

You have your answer in your question only.

  1. pointer to variable , use of *
  2. pointers to pointer of a variable , use **

Details:

** is not a new operator. it's a combination of * and * . In case 2. as per your terminology, you can think of

only single * to point a pointer to another pointer 

as in

int * to an inother int * ==> int **

EDIT:

as per your code

int main()
{
    int a=5,*b,*c;
    b=&a;
    c=&b;
}
  1. b is a pointer to int . You can store the address of int there, and a is an int . Perfect.
  2. c is a pointer to int . You can store the address of int there, and b is a pointer to int . Not accepted.

To make point 2 work, you need to declare c as a pointer to int * , right? The notation for the same is int ** .

Every level of indirection needs a level of dereferencing. So for:

T*** x = ...;

you would need:

***x

to get to T& .

If you had a pointer to pointer and you saved it in:

T* x = ...;
T* y = &x;

it would mean that *ptr leads to T& , while it really leads to another T* .

Here's another way to think of pointers-to-pointers: imagine how it works in memory. Here's a little snippet that shows what I mean:

int TheInteger = 123;
int *p = &TheInteger;
int **pp = &p;

printf("The value at memory location &pp (0x%x) is 0x%x (pp).  This value (which we assigned as &p (0x%x) is 0x%x (p).  This value, in turn, we assign as &TheInegeter (0x%x) points to the 'instance' of TheInteger, which is %d", &pp, pp, &p, p, &TheInteger, TheInteger);

The output of this would be:

The value at memory location &pp (0x657e588) is 0x657e594 (pp).  This value (which we assigned as &p (0x657e594) is 0x657e5a0 (p).  This value, in turn, we assign as &TheInegeter (0x657e5a0) points to the 'instance' of TheInteger, which is 123

Now, to go back to your original question, you cannot declare a variable as being a pointer when the value you're setting it to is a pointer-to-a-pointer. In other words, in your example, you set 'b' as a pointer to a -- so, you can't tell the compiler that 'c' is just a pointer and then try to set it to a value that the compiler knows is a pointer-to-a-pointer.

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