Home > SparkCharts > Cs > C++ > Pointers

C++


 
 

Pointers

A pointer is a variable that contains the memory address of a data structure. In other words, int* a does not contain an integer value. Instead, a would contain a reference to a memory location that is an integer value. Pointer variables can also be used to reference arrays of variables and can be assigned and reassigned during run-time. Pointers can also be used to hold pointers to dynamically assigned memory. Pointers can also be assigned the value of NULL. NULL is a C++ keyword that represents an inaccessible memory location and is commonly used by programmers to represent an unassigned pointer.

Pointer declaration example:

int* a = NULL;

Pointers must be dereferenced before use. This dereferencing is accomplished with the * operator. The & operator can also be used on a nonpointer object to get a reference to that object, as in the example below.

Pointer assignment and dereferencing example:

char b = 'a';
char *a = &b;
cout << *a << end1;