Search code examples
cpointerspointer-to-pointer

pointer to pointer address


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


Solution

  • 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.