Thursday, September 10, 2009

C++ pointer

Reference Operator (&)
The address that locates a variable within memory is what we call a reference to that variable.
A variable which stores a reference to another variable is called a pointer.
& is the reference operator and can be read as "address of"
* is the dereference operator and can be read as "value pointed by"
The declaration of pointers follows this format:
type * name;
 where type is the data type of the value that the pointer is intended to point to. This type is not the type of the pointer itself! but the type of the data the pointer points to.
For example:
int * number;
char * character;
float * realnumber;
These are three declarations of pointers. Each one is intended to point to a different data type, but in fact all of them are pointers and all of them will occupy the same amount of space in memory. Nevertheless, the data to which they point to do not occupy the same amount of space nor are of the same type: the first one points to an int, the second one to a char and the last one to a float.
Void Pointer
The void type of pointer is a special type of pointer. In C++, void represents the absence of type, so void pointers are pointers that point to a value that has no type.

No comments:

Post a Comment