[c++] 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 ???.

This question is related to c++ c

The answer is


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

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


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

you can use this

in C

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

in C++

cout<<p;

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

int * * p_to_p = &p;

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

You can use %p in C

In C:

printf("%p",p)

In C++:

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

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

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.