-2

I have a question about pointer to pointer.

Here's my code

#include <stdio.h>

void main()
{
    int num=10;
    int *numPtr1;
    int **numPtr2;

    numPtr1 = &num;
    numPtr2 = &numPtr1;
    printf("%d\n", num);
    printf("%d\n", *numPtr1);
    printf("%d\n", **numPtr2);
    printf("%p\n", &num);
    printf("%p\n", numPtr1);
    printf("%p", numPtr2);
}

Why numPtr2's address is not the same with numPtr1, numPtr2? For example, let num's address 0x7fffaca780b4. Then when I run this code, the output is

10
10
10
0x7fffaca780b4
0x7fffaca780b4
0x7fffaca780b8

Sorry for my bad english

  • Why don't you make `numPtr1` point at something else and run your program again. Perhaps it will become clear. – paddy Nov 01 '18 at 04:00
  • `numPtr1` has the address of `num`, `numPtr2` has the address to `numPtr1`, thus `*numPtr2` is the address to `num`. – vallentin Nov 01 '18 at 04:02
  • Most of these print statements are undefined behaviour and so are meaningless. The argument to `%d` must have type `int`. The argument to `%p` must have type `void *`. – M.M Nov 01 '18 at 04:35

4 Answers4

1

numPtr1 and numPtr2 are two different variables. So where those variables are located will be different, regardless of where they point to.

H.S.
  • 11,654
  • 2
  • 15
  • 32
Brian Clink
  • 297
  • 2
  • 17
0

Why numPtr2's address is not the same with numPtr1, numPtr2?

Each variable has its own address in memory so their content may be same but their address can't be same otherwise it would be impossible to differentiate them.

suvojit_007
  • 1,690
  • 2
  • 17
  • 23
0

numptr2 is pointing to numptr1 varible,numptr1 is pointing to num. So in numptr2 address of numptr1 will be stored & in numptr1 address of num will be stored both(numptr1,num) addresses are different. this is the reason that the you get different address.

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
Yash Shah
  • 158
  • 2
  • 13
0

About [num]
value of [num] = 10
memory location of [num] = 0115FC14

About [numPtr1]
value of [numPtr1] when it dereferenced = 10
address of [numPtr1] is holding = 0115FC14
memory location of [numPtr1] = 0115FC08

About [numPtr2]
value of [numPtr2] when it dereferenced once = 0115FC14(*numPtr2)
value of [numPtr2] when it dereferenced twice = 10(**numPtr2)
address of [numPtr2] holding = 0115FC08(numPtr2) is equals to memory location of [numPtr1] = 0115FC08(&numPtr1)
memory location of [numPtr2] = 0115FBFC

Name num numPtr1 numPtr2
Value 10 0115FC14 0115FC08
Memory location 0115FC14 0115FC08 0115FBFC

Here is the code

#include <stdio.h>

int main(void)
{
    int a = 5;
    int* a_ptr1 = &a;
    int** a_ptr2 = &a_ptr1;
    puts("***About [a]***");
    printf("value of [a] = %d\nmemory location of [a] = %p\n\n", a, &a);

    puts("***About [a_ptr1]***");
    printf("value of [a_ptr1] when it dereferenced = %d\n", *a_ptr1);
    printf("address of [a_ptr1] is holding = %p\n", a_ptr1);
    printf("memory location of [a_ptr1] = %p\n\n", &a_ptr1);

    puts("***About [a_ptr2]***");
    printf("value of [a_ptr2] when it dereferenced once = %p(*a_ptr2)\n", *a_ptr2);
    printf("value of [a_ptr2] when it dereferenced twice = %d(**a_ptr2)\n", **a_ptr2);
    printf("address of [a_ptr2] holding = %p(a_ptr2) ", a_ptr2);
    printf("is equals to memory location of [a_ptr1] = %p(&a_ptr1)\n", &a_ptr1);
    printf("memory location of [a_ptr2] = %p\n\n", &a_ptr2);


    printf("Name\t\t[a]<---------[a_ptr1]<-------[a_ptr2]\n");
    printf("Value\t\t%d            %p        %p\n", a, a_ptr1, a_ptr2);
    printf("Address\t\t%p     %p        %p\n", &a, &a_ptr1, &a_ptr2);

}
standalone
  • 11
  • 4