[英]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.