简体   繁体   English

如何在c/c++中获取指针的地址?

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

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 ?那么如何获取指针p的地址呢? Now I want to print address of p, what should I do?现在我想打印p的地址,怎么办?

print("%s",???) what I pass to???. print("%s",???) 我传递给什么???。

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;
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* .然后将 a 的地址存储在类型为int* p中。

Finally, if you do &p you get the address of p which is of type int** , ie 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);

&a给出的地址a - &p给出的地址p

int * * p_to_p = &p;

You can use the %p formatter.您可以使用%p格式化程序。 It's always best practice cast your pointer void* before printing.最好的做法是在打印之前将指针void*

The C standard says: C标准说:

The argument shall be a pointer to void.参数应是指向 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);

You can use %p in C你可以在 C 中使用 %p

In C:在 C 中:

printf("%p",p)

In C++:在 C++ 中:

cout<<"Address of pointer p is: "<<p

you can use this你可以用这个

in C在 C

int a =10;
int *p = &a;     
int **pp = &p;
printf("%u",&p);

in C++在 C++ 中

cout<<p;

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 and it is defined to be used with void* typed pointers only.请注意,转换说明符p是唯一用于打印指针值的转换说明符它被定义为仅与void*类型的指针一起使用。

From man printf :man printf

p

The void * pointer argument is printed in hexadecimal (as if by % #x or % #lx ). void *指针参数以十六进制打印(就像按 % #x或 % #lx 一样)。

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

First, you should understand the pointer is not complex.首先,您应该了解指针并不复杂。 A pointer is showing the address of the variable.指针显示变量的地址。

Example:例子:

int a = 10;
int *p = &a;  // This means giving a pointer of variable "a" to int pointer variable "p"

And, you should understand "Pointer is an address" and "address is numerical value".并且,您应该理解“指针是地址”和“地址是数值”。 So, you can get the address of variable as Integer.因此,您可以将变量的地址作为整数获取。

int a = 10;
unsigned long address = (unsigned long)&a;

// comparison
printf("%p\n", &a);
printf("%ld\n", address);

output is below输出低于

0x7fff1216619c
7fff1216619c

Note:笔记:

If you use a 64-bit computer, you can't get pointer by the way below.如果你使用的是64位的电脑,你不能通过下面的方式得到指针。

int a = 10;
unsigned int address = (unsigned int)&a;

Because pointer is 8 bytes (64 bit) on a 64-bit machine, but int is 4 bytes.因为指针在 64 位机器上是 8 个字节(64 位),而 int 是 4 个字节。 So, you can't give an 8-byte memory address to 4 bytes variable.因此,您不能为 4 字节变量提供 8 字节内存地址。

You have to use long long or long to get an address of the variable.您必须使用long longlong来获取变量的地址。

  • long long is always 8 bytes. long long总是 8 个字节。
  • long is 4 bytes when code was compiled for a 32-bit machine.为 32 位机器编译代码时, long为 4 个字节。
  • long is 8 bytes when code was compiled for a 64-bit machine.为 64 位机器编译代码时, long为 8 个字节。

Therefore, you should use long to receive a pointer.因此,您应该使用long来接收指针。

If you are trying to compile these codes from a Linux terminal, you might get an error saying如果您尝试从 Linux 终端编译这些代码,您可能会收到一条错误消息:

expects argument type int期望参数类型 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.这是因为,当您尝试通过printf获取内存地址时,您无法将其指定为%d如视频中所示。 Instead of that try to put %p .而不是尝试放置%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); 

Building off the above answers, if you had the pointer as a private variable, then you can make a getter function like so:基于上述答案,如果您将指针作为私有变量,那么您可以像这样创建一个 getter 函数:

private int* p;

public int** GetP() { return &p; }

Then when you use it you create a pointer to your class that contains it:然后当你使用它时,你会创建一个指向包含它的类的指针:

DummyClass* p_dummyClass = new DummyClass;

And then in your use case scenario:然后在您的用例场景中:

p_dummyClass -> GetP();

(don't forget to deallocate) (不要忘记释放)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM