Trung Nguyen
Trung Nguyen

Reputation: 868

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

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 address of pointer p? Now I want to print address of p, what should I do?

print("%s",???) what I pass to ???.

Upvotes: 47

Views: 195932

Answers (12)

Mahdi Mohammadi
Mahdi Mohammadi

Reputation: 297

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;
}

Upvotes: -2

Vallisneria
Vallisneria

Reputation: 13

Building off the above answers, if you had the pointer as a private variable, then you can make a getter function like so:

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)

Upvotes: 0

Sandagomi
Sandagomi

Reputation: 1

If you are trying to compile these codes from a Linux terminal, you might get an error saying

expects argument type 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. Instead of that try to put %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); 

Upvotes: 0

JUN_NETWORKS
JUN_NETWORKS

Reputation: 361

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.

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. So, you can't give an 8-byte memory address to 4 bytes variable.

You have to use long long or long to get an address of the variable.

  • long long is always 8 bytes.
  • long is 4 bytes when code was compiled for a 32-bit machine.
  • long is 8 bytes when code was compiled for a 64-bit machine.

Therefore, you should use long to receive a pointer.

Upvotes: 1

Hamid Yusifli
Hamid Yusifli

Reputation: 10137

In C++ you can do:

// 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

Upvotes: 1

Arjun Sreedharan
Arjun Sreedharan

Reputation: 11453

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);

Upvotes: 2

alk
alk

Reputation: 70931

Having this C source:

int a = 10;
int * ptr = &a;

Use this

printf("The address of ptr is %p\n", (void *) &ptr);

to print the address of 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.

From man printf:

p

The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx).

Upvotes: 2

zmo
zmo

Reputation: 24812

int a = 10;

To get the address of a, you do: &a (address of a) which returns an int* (pointer to int)

int *p = &a;

Then you store the address of a in p which is of type int*.

Finally, if you do &p you get the address of p which is of type int**, i.e. pointer to pointer to 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("address of pointer is: %p\n", p);
printf("address of pointer to pointer is: %p\n", p_ptr);

Upvotes: 23

Loghorn
Loghorn

Reputation: 2807

To get the address of p do:

int **pp = &p;

and you can go on:

int ***ppp = &pp;
int ****pppp = &ppp;
...

or, only in C++11, you can do:

auto pp = std::addressof(p);

To print the address in C, most compilers support %p, so you can simply do:

printf("addr: %p", pp);

otherwise you need to cast it (assuming a 32 bit platform)

printf("addr: 0x%u", (unsigned)pp);

In C++ you can do:

cout << "addr: " << pp;

Upvotes: 102

Rishi Dwivedi
Rishi Dwivedi

Reputation: 928

you can use this

in C

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

in C++

cout<<p;

Upvotes: 2

user3345354
user3345354

Reputation: 105

You can use %p in C

In C:

printf("%p",p)

In C++:

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

Upvotes: 2

Sadique
Sadique

Reputation: 22821

&a gives address of a - &p gives address of p.

int * * p_to_p = &p;

Upvotes: 7

Related Questions