A pointer is just what it sounds like, a pointer to a place in memory.
If you declare an integer like below.
You have an integer variable. If you declare a pointer to an integer as below...
You have a pointer that is pointing at nothing, 0 = NULL. This is what's called a NULL pointer.
In order to make the pointer valid, you must make it point at some valid memory. There are a couple of different ways to make this pointer point at valid memory. First of all there is allocating new memory.
Code:
int * pValue1 = new int;
Now you have a pointer pointing at a single integer variable.
To access the memory at this location, you have to dereference the pointer. That is done as follows.
Code:
int * pValue1 = new int;
*pValue1 = 0; // now the integer pointed at by pValue1 is zero.
delete pValue1; // we must cleanup the memory we used by dynamically allocating it.
As I posted in the above code section was the delete operator. You must free the memory allocated by new with delete.
Another way to use a pointer is by taking the address of a variable using the the prefix & operator. This is the address of operator. You can use it as follows, and the pointer will point at the same variable as the integer I declare.
Code:
int nValue1 = 100;
int * pValue1 = &nValue1;
*pValue1 = 200;
if (nValue1 == 200)
{
std::cout << "pValue1 points at the same place in memory as nValue1. It's value is: " << nValue1 << std::endl;
}
// Since pValue1 points at an integer value on the stack, there's no need to delete pValue1.
Pointers can also point at dynamic arrays of memory, and to declare a pointer pointing at an array of memory you can do the following. This introduces the new[] and delete[] operators
Code:
int * pArray1 = new int[100]; // we have pArray1 pointing at 100 integers in memory. The memory is contiguous.
// initialize the array of memory with the corresponding index into the array.
for (int x = 0; x < 100; ++x)
{
pArray1[x] = x;
}
// Now pArray1 is filled with 100 integers in a row in ascending order from 0 to and including 99.
// now delete [] the memory used by pArray1.
delete [] pArray1;
That is for primitive types. I don't know if you are far enough into learning C++ to learn about objects from classes, but you can have pointers to any type in C++, whether it's an integral integer or a complex polymorphic derived class pointed at by a base pointer.
If you want me to post on how to use polymorphic objects pointed at by pointers reply to this thread and I will give you an example of using polymorphism.
EDIT...
Sometimes you need access to a variable in a function from another function. By using a pointer we can access the variable we want to access from another function.
Code:
void Function2(int * pValue)
{
*pValue = 100;
}
void Function1()
{
int nValue = 0;
Function2(&nValue); // using the address of operator to create a temporary pointer pushed onto the stack.
if (nValue == 100)
{
std::cout << "nValue is now 100 set by Function2." << std::endl;
}
}
You can pass an array pointed at by a pointer across function boundaries. You should specify the length of the array as a second parameter.
Code:
void InitializeArray(int * pArray,int nLength)
{
for (int x = 0; x < nLength; ++x)
{
pArray[x] = 100 + x;
}
}
void Function1()
{
// allocate an array of memory.
int * pArray = new array[100];
// initialize the array
InitializeArray(pArray,100);
for (unsigned int x = 0; x < 100; ++x)
{
std::cout << "pArray[" << x << "] = " << pArray[x] << std::endl;
}
// free the memory pointed at by pArray
delete [] pArray;
}