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

前端 未结 10 1874
逝去的感伤
逝去的感伤 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);
    
    0 讨论(0)
  • 2020-12-07 15:27

    To get the address of p do:

    int **pp = &p;
    

    and you can go on:

    int ***ppp = &pp;
    int ****pppp = &ppp;
    ...
    

    or, only in C++11, you can do:

    auto pp = std::addressof(p);
    

    To print the address in C, most compilers support %p, so you can simply do:

    printf("addr: %p", pp);
    

    otherwise you need to cast it (assuming a 32 bit platform)

    printf("addr: 0x%u", (unsigned)pp);
    

    In C++ you can do:

    cout << "addr: " << pp;
    
    0 讨论(0)
  • 2020-12-07 15:33

    Having this C source:

    int a = 10;
    int * ptr = &a;
    

    Use this

    printf("The address of ptr is %p\n", (void *) &ptr);
    

    to print the address of ptr.

    Please note that the conversion specifier p is the only conversion specifier to print a pointer's value and it is defined to be used with void* typed pointers only.

    From man printf:

    p

    The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx).

    0 讨论(0)
  • 2020-12-07 15:33

    If you are trying to compile these codes from a Linux terminal, you might get an error saying

    expects argument type int

    Its because, when you try to get the memory address by printf, you cannot specify it as %d as its shown in the video. Instead of that try to put %p.

    Example:

    // this might works fine since the out put is an integer as its expected.
    printf("%d\n", *p); 
    
    // but to get the address:
    printf("%p\n", p); 
    
    0 讨论(0)
  • 2020-12-07 15:34

    You can use the %p formatter. It's always best practice cast your pointer void* before printing.

    The C standard says:

    The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

    Here's how you do it:

    printf("%p", (void*)p);
    
    0 讨论(0)
  • 2020-12-07 15:37

    &a gives address of a - &p gives address of p.

    int * * p_to_p = &p;
    
    0 讨论(0)
提交回复
热议问题