-4

Here is code and its output:

#include <iostream>
using namespace std; 

int main()
{
    int number = 8; 
    int* ptr = &number; 

    cout <<"Value of *ptr = "<<*ptr<<endl;
    cout <<"Value of ptr = "<<ptr<<endl;
    cout <<"Value of &number = "<<&number<<endl; 
    cout <<"Value of &ptr = "<<&ptr<<endl;

    return 0; 
}

Output

Why does the value of ptr and &ptr differ? what is the reason I'm just curious to know

  • 7
    Why would you expect the address of a pointer variable to be the same as the address it points to?? – πάντα ῥεῖ Mar 29 '21 at 08:51
  • And why would you expect two different syntaxes to mean the same thing? What would be the point? – user207421 Mar 29 '21 at 10:20
  • A pointer like `int*` stores an address to an int. `number` is an int, and that variable has its own address. `ptr` has the value of the address of `number`. `ptr` itself as a variable (a pointer) also has its own address. The address of the two variables are different. There are only a few circumstances where two variables can have the same address. – Eljay Mar 29 '21 at 11:04
  • question is actually why the value of 'ptr' and '&ptr' differ – Bilal Mohmand Mar 30 '21 at 16:56
  • `ptr` and `&ptr` are two different things, so why *wouldn't* you expect them to have different values? The value of `ptr` is the address of an `int` located somewhere in memory. `&ptr` is the memory address of `ptr` itself. – Remy Lebeau Mar 30 '21 at 17:21

3 Answers3

1

You have two variables number and ptr.

&number gives a pointer to the number variable, and &ptr gives a pointer to the ptr variable.

*ptr dereferences the value of ptr, which in this case is &number, and returns the value of that number.

&ptr gives you a pointer to the value inside ptr. The value of &ptr is a pointer to a pointer.

int number = 8; 
int* ptr = &number; 

// prints the value of the pointer stored in ptr which is number
cout << "Value of *ptr = " << *ptr << endl;

// prints the pointer stored in ptr which is &number
cout << "Value of ptr = " << ptr << endl;

// prints the pointer to number
cout << "Value of &number = " << &number <<endl;

// prints the pointer to the pointer stored in ptr
cout << "Value of &ptr = " << &ptr << endl;
Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
Saurus
  • 46
  • 3
0

ptr is a pointer to an int in memory (in this case, it is pointing at the number variable).

*ptr is dereferencing ptr to access the value of the int it is pointing at.

&ptr is getting a pointer to (taking the memory address of) ptr itself, not of the int it is pointing at.

Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
0

With *ptr, you are 'dereferencing' the variable ptr, that is from the pointer (basically a memory address) you are retrieving the value of the pointed memory space (in this case, the value of number).

With &ptr, you are retrieving the pointer to a pointer, in this case to dereference it, and finally retrieve the value of number you would have to type **ptr.

You can check the official docs here:

https://www.cplusplus.com/doc/tutorial/pointers/

Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
Nazgot
  • 308
  • 2
  • 16