How to get address of a pointer in c/c++?

前端 未结 10 1897
逝去的感伤
逝去的感伤 2020-12-07 14:43

How to get address of a pointer in c/c++?

Eg: I have below code.

int a =10;
int *p = &a;

So how do I get addre

10条回答
  •  一整个雨季
    2020-12-07 15:23

    int a = 10;
    

    To get the address of a, you do: &a (address of a) which returns an int* (pointer to int)

    int *p = &a;
    

    Then you store the address of a in p which is of type int*.

    Finally, if you do &p you get the address of p which is of type int**, i.e. pointer to pointer to int:

    int** p_ptr = &p;
    

    just seen your edit:

    to print out the pointer's address, you either need to convert it:

    printf("address of pointer is: 0x%0X\n", (unsigned)&p);
    printf("address of pointer to pointer is: 0x%0X\n", (unsigned)&p_ptr);
    

    or if your printf supports it, use the %p:

    printf("address of pointer is: %p\n", p);
    printf("address of pointer to pointer is: %p\n", p_ptr);
    

提交回复
热议问题