C++: Pointers
What a Java Programmer should know about Pointers
Accessing Memory Locations with Pointers
C++ operators for Accessing Memory
| Operator |
Name |
Function |
& |
address operator |
provides the memory address of a given variable |
| * |
dereference operator |
provides the value stored in a specfic memory address |
Download and run this Pointer Demo Code
#include
using namespace std;
int main()
{
int x = 25;
cout << "\n the value of x is " << x <<endl;
cout<< "\n the address of x is : "<< &x <<endl;
//now lets declare a points
int *pointsToX;
pointsToX = &x;
cout<< "\n pointsTox : " << pointsToX <<endl;
cout<< "\n Now let's use the pointer to get the value it 'points' to " <<endl;
cout<< "\npointsTox points to " << *pointsToX <<endl;
//try to uncomment the next line...watch what happens
// pointsToX = x;
return 0;
}
You can see the output that this program on my computer. Although the address of x may be different on your computer, you should get ouput similar to that below.