Saturday, April 11, 2015

C++ Pointers


Pointer contains address of another variable or object.  To point to an existing variable, assign the point using the address operator (&)

int a
int* b = &a

To access the variable pointed by a pointer, use the dereference operator (*)

int c = *b

A constant pointer cannot change to point to another variable

int* const b = &a
b = &c // generates error

A pointer to a constant cannot change the value of the variable the pointer points to

const int* b
*b = c // generates error

a constant pointer to a constant is most restrictive
const int* const b

No comments: