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

前端 未结 10 1907
逝去的感伤
逝去的感伤 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: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);
    

提交回复
热议问题