AngryA hard question

Posts 1–4 of 4 · Page 1 of 1
A hard question
If i initialized a pointer in a code, it has a memory address and a value,
Lets say the pointer's name is Pointer
NOW : What is the difference between when we cout Pointer and when we cout &Pointerr ???
As they give me 2 different memories..

N.B(If we cout *Ptr it will just cout it's values, like if *Ptr = 3, it will show 3)
Copy and paste the code below and u will understand me, cout *pointer gives 5.99, pointer and &x gives same memory address BUT &pointer gives a different one, That's my question: WHY DIFFERET MEMORY ADDRESS??

Code:
#include <iostream>             
using namespace std;
double *temp;
void SevenPointThree(double *temp)
{                                
	*temp=7.3;        
}
int main()
{
	
	double x=5.99;
	double *pointer = &x;
		cout<<*pointer<<endl;
	    cout<<pointer<<endl;
		cout<<&x<<endl;
		cout<<&pointer<<endl;
    
	
	cin.get();
	return 0;
}
Quote Originally Posted by meromarololo2 View Post
If i initialized a pointer in a code, it has a memory address and a value,
...
Even though 'Pointer' is a pointer (duh), and you generally think of it as "a memory address", it is itself a variable, and has to exist somewhere in ram.
&Pointer = where the variable Pointer itself lives.
Pointer = the address stored in those cubby boxes.

Code:
double x=5.99;
double *pointer = &x;

cout<<*pointer<<endl; //5.99
cout<<pointer<<endl;  //some mem addr, where x is stored
cout<<&x<<endl;  //same mem addr as abv., where x is stored
cout<<&pointer<<endl;  //where 'pointer' is stored, a separate location from x (but likely very close as they're declared in the same function, off by sizeof(double) I'd imagine.
&Pointer is where this pointer variable located in the ram.
Pointer is the variable.
If you have an pointer, as name says it's point to some memory location, that's why when you cout << pointer it's give same memory location as &x, because it's point to him location but obvious the pointer needs an location at memory as it can't belong to the same location that the variable him's pointing at. Explaining in code
Code:
 
int  x = 3; // 0x0001
int *pointer = &x; //0x0002
// so 
cout << &x << endl << pointer << endl << &pointer << endl;
// It will give an output close to it: 
//0x7fff314290ac x Memory location
//0x7fff314290ac pointer pointing location
//0x7fff314290a0 pointer Memory location
If I'm not wrong. That's it.
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us