-1

I just studying pointers in C and I wondered what is the address of a pointer and after I searched on the Internet I wrote this piece of code.

#include <stdio.h>

int main() {
    int n = 60;
    int *p = &n;
    int **p_p = &p;
    printf("%p\n", *p_p);
    printf("%p", &p);
    return 0;
} 

and I expected two outputs which are the same. Could anyone explain why outputs differ?

Ulaş Sezgin
  • 308
  • 2
  • 10
  • The value of `p_p` is `&p`. So `*p_p` points to whatever `&p` points to (It's `*(&p)`), which is `&n`. Try `printf("%p\n", p_p)` instead of `printf*("%p\n", *p_p)`. – lurker Jun 08 '20 at 17:03
  • They only *point to* the same value. It is not very different from two variables `x = 10; y = 10;` – even though their value is "the same", changing the value of one does not change the other. (With pointers, though, you'd get a different result on changing.) – Jongware Jun 08 '20 at 17:04

6 Answers6

1

A pointer is a variable like any other, as such it needs to be stored, the address of a pointer is the address of the memory location where the pointer is stored.

The value of the pointer is the address of the variable it points to so the value of p is the address of n and the value of p_p is the address of p.

When you dereference p_p you get the value of p not the address of p, and as the value of p is the address of n the value of *p_p is the address of n.

So

printf("%p\n", *p_p);
printf("%p\n", p);
printf("%p", &n);

will print the exact same value, the address of n.

Printing &p will print the address of p.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
1

A pointer variable has its own address, just like any other variable. The fact that it can store an address doesn't change that.

The variable n has an address (for example, 100) stores a value of type int, which in this case is 60.

The variable p also has an address (for example, 104) and stores a value of type int * which in this case would be 100, i.e. the address of n.

Similarly, the variable pp has an address (for example, 108) and stores a value of type int ** which in this case would be 104, i.e. the address of p.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

&p is the address of p.

p_p is the address of p, due to the definition int **p_p = &p;.

*p_p is the thing p_p points to. Since p_p points to p, *p_p is p.

When you print &p, it prints the address of p.

When you print p_p, it prints the address of p.

When you print *p_p, it prints the value of p, which is the address of n.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1
#include <stdio.h>

int main() {
    int n = 60;
    int *p = &n;
    int **p_p = &p;
    printf("%p\n", *p_p); //means the value inside of pointer p which means(address of n)  
    printf("%p\n", p_p);// address of pointer p
    printf("%p", &p); // also address of pointer p
    return 0;
} 
``````````````````````
sara hamdy
  • 402
  • 2
  • 12
0

**p_p=&p it means that p_p is pointer to pointer so if you print p_p only it will give you the value inside it which means address of pointer p but if you print *p_p it means the value inside pointer p which means address of n so if you want to print the address of pointer p by using different two method

printf("%p\n", p_p);// address of pointer p
 printf("%p", &p); // also address of pointer p

but

printf("%p\n", *p_p); // the value inside of pointer p which means (address of n)
`````````````
 means the value inside pointer p which means address of n

sara hamdy
  • 402
  • 2
  • 12
0

pointers are like any other data type in C programming language. pointers have names and values and when they born we can initialize them or not.

The special property in pointers is their values. They are designated to hold/contain the address of other data type, But that does not contradict the fact that they are also variables that lives in certain address in the memory/ram!

Consider the code snippet below:

int x = 10;

x is data type, his type is integer and its value is 10

int* p_x = &x; 

p_x is data type, his type is int* (pointer to int) and his value is the address of x (&x)

int* p; 

p is data type, his type is int* (pointer to int) and his value is unknown (valid address in the system). this is very bad. it called dangling pointer and rule number one, when you declare a pointer you initialize it immediately (NULL is used often if you don't have a valid address for initializing in declaration time). please consider that compilers are smart and can initialize uninitialized pointer to null. but never relay on this.

int** p_px = &p_x;

p_px is data type, his type is int** (pointer to int*, OR pointer to int pointer) and his value is the address of p_x (&p_x), where p_x is found in ram.

Let's see an Illustration of the above variables in RAM:

*---------------------------------------------------------*
*      Address       |      Value         |    var Name   *
*---------------------------------------------------------*
* 0x7fff4ee2095c     |       10           |      x        *
*---------------------------------------------------------*
* 0x7fff4ee20960     |  0x7fff4ee2095c    |     p_x       *
*---------------------------------------------------------*
* 0x7fff4ee20968     |   0x7fff4ee20960   |     p_px      *
*---------------------------------------------------------*
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Adam
  • 2,820
  • 1
  • 13
  • 33