[英]How to get address of a pointer in c/c++?
如何在C/C++中获取指针的地址?
例如:我有以下代码。
int a =10;
int *p = &a;
那么如何获取指针p
的地址呢? 现在我想打印p的地址,怎么办?
print("%s",???) 我传递给什么???。
要获取 p 的地址,请执行以下操作:
int **pp = &p;
你可以继续:
int ***ppp = &pp;
int ****pppp = &ppp;
...
或者,仅在 C++11 中,您可以执行以下操作:
auto pp = std::addressof(p);
要在 C 中打印地址,大多数编译器都支持%p
,因此您可以简单地执行以下操作:
printf("addr: %p", pp);
否则你需要投射它(假设是 32 位平台)
printf("addr: 0x%u", (unsigned)pp);
在 C++ 中,您可以执行以下操作:
cout << "addr: " << pp;
int a = 10;
要获取 a 的地址,您可以执行以下操作: &a
(address of a
) 返回一个int*
(指向 int 的指针)
int *p = &a;
然后将 a 的地址存储在类型为int*
p
中。
最后,如果你这样做&p
你的地址p
这型的int**
,即指针指向INT:
int** p_ptr = &p;
刚刚看到你的编辑:
要打印出指针的地址,您需要转换它:
printf("address of pointer is: 0x%0X\n", (unsigned)&p);
printf("address of pointer to pointer is: 0x%0X\n", (unsigned)&p_ptr);
或者如果您的 printf 支持它,请使用%p
:
printf("address of pointer is: %p\n", p);
printf("address of pointer to pointer is: %p\n", p_ptr);
&a
给出的地址a
- &p
给出的地址p
。
int * * p_to_p = &p;
您可以使用%p
格式化程序。 最好的做法是在打印之前将指针void*
。
C标准说:
参数应是指向 void 的指针。 指针的值以实现定义的方式转换为打印字符序列。
这是你如何做到的:
printf("%p", (void*)p);
你可以在 C 中使用 %p
在 C 中:
printf("%p",p)
在 C++ 中:
cout<<"Address of pointer p is: "<<p
你可以用这个
在 C
int a =10;
int *p = &a;
int **pp = &p;
printf("%u",&p);
在 C++ 中
cout<<p;
拥有这个 C 源代码:
int a = 10;
int * ptr = &a;
用这个
printf("The address of ptr is %p\n", (void *) &ptr);
打印ptr
的地址。
请注意,转换说明符p
是唯一用于打印指针值的转换说明符,它被定义为仅与void*
类型的指针一起使用。
磷
void *指针参数以十六进制打印(就像按 % #x或 % #lx 一样)。
在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
首先,您应该了解指针并不复杂。 指针显示变量的地址。
例子:
int a = 10;
int *p = &a; // This means giving a pointer of variable "a" to int pointer variable "p"
并且,您应该理解“指针是地址”和“地址是数值”。 因此,您可以将变量的地址作为整数获取。
int a = 10;
unsigned long address = (unsigned long)&a;
// comparison
printf("%p\n", &a);
printf("%ld\n", address);
输出低于
0x7fff1216619c
7fff1216619c
笔记:
如果你使用的是64位的电脑,你不能通过下面的方式得到指针。
int a = 10;
unsigned int address = (unsigned int)&a;
因为指针在 64 位机器上是 8 个字节(64 位),而 int 是 4 个字节。 因此,您不能为 4 字节变量提供 8 字节内存地址。
您必须使用long long
或long
来获取变量的地址。
long long
总是 8 个字节。long
为 4 个字节。long
为 8 个字节。 因此,您应该使用long
来接收指针。
如果您尝试从 Linux 终端编译这些代码,您可能会收到一条错误消息:
期望参数类型 int
这是因为,当您尝试通过printf
获取内存地址时,您无法将其指定为%d
如视频中所示。 而不是尝试放置%p
。
例子:
// 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);
基于上述答案,如果您将指针作为私有变量,那么您可以像这样创建一个 getter 函数:
private int* p;
public int** GetP() { return &p; }
然后当你使用它时,你会创建一个指向包含它的类的指针:
DummyClass* p_dummyClass = new DummyClass;
然后在您的用例场景中:
p_dummyClass -> GetP();
(不要忘记释放)
static inline unsigned long get_address(void *input){
unsigned long ret;
__asm__(
".intel_syntax noprefix;"
"mov rax, %1;"
"mov %0, rax;"
".att_syntax;"
: "=r"(ret)
: "r"(input));
return ret;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.