Table of Contents

Pointer in C++

Basic concepts

A pointer contains the memory address of a variable.

// Declare a pointer to a int variable.
int *pnPtr;

Address-of operator: &

Store the address of a variable in a pointer, by using the address of operator:

// From value to pointer
int nValue = 42;
int *pnPtr = &nValue;

Dereference operator: *

Given a pointer, retrieve the value.

cout << *pnPtr;

Null-Pointer

int *pnPtr = 0;
// also possible, but this is C
int *pnPtr = NULL;

Used in an if condition:

if (pnPtr) 
  cout << "pointer not null"; 
else 
  cout << "null-pointer";

Size of a pointer depends on the underlying OS/system (32/64 bit ⇒ 4 byte/8 byte).

Pointer and Arrays

An array is a pointer which refers to the first element of an array.

int anArray[3] = {3, 5, 7};
cout << *anArray; // Gibt erstes Element "3" zurück

Pointer arithmetic

If pnPtr points to an integer, pnPtr + 1 is the address of the next integer.

pnPtr + 1 does not return the address + 1, instead it returns the address of the object of the type pnPtr points to.

Example.: pnPtr points to an integer (4 bytes) pnPtr + 2 means 2 integers after pnPtr, which results in 8 adresses after pnPtr.

The data type size can be returned with sizeof().

Scaling: The compiler multiplies the integer operand with the size of the object which is pointed to.

Array example:

Given the following example:

int anArray[3] = {3, 5, 7};

anArray is a pointer to anArray[0] (it is &anArray[0]). anArray + 1 is a pointer to anArray[i].

Access to the second element of the above array:

int anArray[3] = {3, 5, 7};
cout << *(anArray + 1); // Returns element "5"

Dynamic memory allocation

Einzelne Variablen

int *pnValue = new int;

new-Operator allocates space on the heap and returns the address of the variable which was allocated.

Release allocated memory

delete pnValue; // Delete memory on which the pointer points
pnValue = 0; // Set null pointer

Arrays

// Speicher allokieren
int *pnArray = new int[10];
// Freigeben
delete[] pnArray;
pnArray = 0;

Multidimensional arrays

Since an array is a pointer, a array of array can be seen as a pointer to pointers.

A 2dimensional array can declared as follows:

int x = 5;
int y = 5;
// int* arr would declare a pointer to a array of ints
int** arr = new int*[x]; // declares a pointer to an array of pointers
for(int i = 0; i < x; ++i)
   arr[i] = new int[y];

http://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function

const

Konstanter Pointer:

int nValue = 1;
int *const pnPtr = &nValue;

nValue ist nicht konstant, und Wert von nValue kann mit *pnPtr = 2 geändert werden.

Referenzen

Konstante, die als Alias für eine andere Variable dient

int nValue = 5;
int &rnRef = nValue;
nValue++;
cout << rnRef; // 6

Konstante (Const) Referenzen verbieten Änderung des Wertes der Referenz

const int &rnRef = nValue;

Member Selection

(*psSomething).nValue = 5;
psSomething->nValue = 5;

Function pointer

// Pointer f to a function, which returns void and has no parameters
void (*f) (void)
// Pointer f to a function, which returns a void pointer and has a void pointer as parameter
void *(*f)(void *)
// Function can be called like this:
f();
// Pointer func2, which returns a pointer to a char and takes a pointer to a const char and an int as a parameter.
char * (*func2)(const char *, int) = strchr;