如何在c/c++中获取指针的地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22250067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get address of a pointer in c/c++?
提问by Trung Nguyen
How to get address of a pointer in c/c++?
如何在c/c++ 中获取指针的地址?
Eg: I have below code.
例如:我有以下代码。
int a =10;
int *p = &a;
So how do I get address of pointer p
?
Now I want to print address of p, what should I do?
那么如何获取指针的地址p
呢?现在我想打印 p 的地址,我该怎么办?
print("%s",???) what I pass to ???.
print("%s",???) 我传递给 ???.
回答by Loghorn
To get the address of p do:
要获取 p 的地址,请执行以下操作:
int **pp = &p;
and you can go on:
你可以继续:
int ***ppp = &pp;
int ****pppp = &ppp;
...
or, only in C++11, you can do:
或者,仅在 C++11 中,您可以执行以下操作:
auto pp = std::addressof(p);
To print the address in C, most compilers support %p
, so you can simply do:
要在 C 中打印地址,大多数编译器都支持%p
,因此您可以简单地执行以下操作:
printf("addr: %p", pp);
otherwise you need to cast it (assuming a 32 bit platform)
否则你需要投射它(假设是 32 位平台)
printf("addr: 0x%u", (unsigned)pp);
In C++ you can do:
在 C++ 中,您可以执行以下操作:
cout << "addr: " << pp;
回答by zmo
int a = 10;
To get the address of a, you do: &a
(address of a
) which returns an int*
(pointer to int)
要获取 a 的地址,您可以执行以下操作:&a
(address of a
) 返回一个int*
(指向 int 的指针)
int *p = &a;
Then you store the address of a in p
which is of type int*
.
然后存储p
类型为 的 a 的地址int*
。
Finally, if you do &p
you get the address of p
which is of type int**
, i.e. pointer to pointer to int:
最后,如果你这样做,&p
你会得到p
类型为的地址int**
,即指向 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 支持它,请使用%p
:
printf("address of pointer is: %p\n", p);
printf("address of pointer to pointer is: %p\n", p_ptr);
回答by Sadique
&a
gives address of a
- &p
gives address of p
.
&a
给出地址a
-&p
给出地址p
。
int * * p_to_p = &p;
回答by Arjun Sreedharan
You can use the %p
formatter. It's always best practice cast your pointer void*
before printing.
您可以使用%p
格式化程序。void*
在打印之前投射指针始终是最佳实践。
The C standard says:
C标准说:
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.
参数应是指向 void 的指针。指针的值以实现定义的方式转换为打印字符序列。
Here's how you do it:
以下是您的操作方法:
printf("%p", (void*)p);
回答by user3345354
You can use %p in C
你可以在 C 中使用 %p
In C:
在 C 中:
printf("%p",p)
In C++:
在 C++ 中:
cout<<"Address of pointer p is: "<<p
回答by Rishi Dwivedi
you can use this
你可以用这个
in C
在 C
int a =10;
int *p = &a;
int **pp = &p;
printf("%u",&p);
in C++
在 C++ 中
cout<<p;
回答by alk
Having this C source:
拥有这个 C 源代码:
int a = 10;
int * ptr = &a;
Use this
用这个
printf("The address of ptr is %p\n", (void *) &ptr);
to print the address of ptr
.
打印地址ptr
。
Please note that the conversion specifier p
is the only conversion specifier to print a pointer's value andit is defined to be used with void*
typed pointers only.
请注意,转换说明符p
是唯一用于打印指针值的转换说明符,它被定义为仅用于void*
类型化指针。
From man printf
:
p
The void *pointer argument is printed in hexadecimal (as if by %#xor %#lx).
磷
的无效*指针参数被印刷以十六进制(仿佛%#x中或%#lx)。
回答by 0xBFE1A8
In C++you can do:
在C++ 中,您可以执行以下操作:
// Declaration and assign variable a
int a = 7;
// Declaration pointer b
int* b;
// Assign address of variable a to pointer b
b = &a;
// Declaration pointer c
int** c;
// Assign address of pointer b to pointer c
c = &b;
std::cout << "a: " << a << "\n"; // Print value of variable a
std::cout << "&a: " << &a << "\n"; // Print address of variable a
std::cout << "" << "" << "\n";
std::cout << "b: " << b << "\n"; // Print address of variable a
std::cout << "*b: " << *b << "\n"; // Print value of variable a
std::cout << "&b: " << &b << "\n"; // Print address of pointer b
std::cout << "" << "" << "\n";
std::cout << "c: " << c << "\n"; // Print address of pointer b
std::cout << "**c: " << **c << "\n"; // Print value of variable a
std::cout << "*c: " << *c << "\n"; // Print address of variable a
std::cout << "&c: " << &c << "\n"; // Print address of pointer c