To elaborate on what pankajkumar wrote, *variable creates a "pointer" to an address in memory.
int *ptr; //creates a pointer to an integer
------------------------------------------------------
The & operator returns an address in memory. Let's say int myInt has an address of 0001 (just pretend).
int myInt = 5;
&myInt; //would return 0001, not 5
--------------------------------------------------------
Since ptr "points" to an address,
ptr = &myInt; //assigns the address of myInt to the pointer
//Again, since ptr points to an address
cout << ptr << endl; //would print out 0001, not 5
---------------------------------------------------------
To "dereference" a variable (get its value instead of it's address) use the * operator like so:
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.