The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. ― Donald E. Knuth

# C++ Tutorial

## C++ Pointers

 Module Index ► What is a Pointer ? ► Pointer to a Pointer ► void type Pointer ► NULL Pointer ► Pointer Arithmetic ► Function Pointers

Pointer is one of the most fascinating aspects of C++ programming language. Mastering the clever use of pointers is a gateway to become a geek in C++. For the beginners, it is considered to be the most difficult concept to understand but it is just a myth built over time. Learning pointers is fun and it lets you realize and appreciate the power of C++ language. We introduce the pointers here and you will find it being used while exploring many aspects and features of C++.

What is a Pointer ?
Consider the following declaration :

 int x = 13;

This declaration reserves a space of 4 bytes (integer) in memory. That memory location is named ' x ' and a value 13 is stored there. Now, every memory location has an address. Pointers are a means to store and access the address of this memory location. In simple words, a pointer variable stores the address of another variable. Note that every address is represented by a number in hex notation.
Following program demonstrate the concept :

In the above program, we declared a pointer variable ( ptr ) of integer type. This indicates that this variable can store the address of an integer variable only. Next we store the address of another variable ( x ) in the pointer variable ( ptr ). Next set of statements show different ways of knowing the address and value of variable x.

Pointer to a Pointer
A pointer variable stores address of another variable. It can even store the address of a pointer variable. This is where the concept of pointer to pointer comes into play. Following program demonstrates the concept :

We just need to remember that ' & ' denotes address of a variable and ' * ' denotes value at a particular address.
A statement like int ***k is also legal. It simple means it stores the address of pointer to a pointer variable. From the above program, j = &i. Similarly, k = &j.

void type Pointer
An integer pointer (int *ptr) can store an address of integer type variable (int x) only. Similarly, a floating point ptr (float *ptr) can store an address of float variable only (float x). This is valid for all data types except void which can store address of a variable of any data type. Following program illustrates the use of void type pointer :

NULL Pointer
A pointer variable can be assigned a value NULL indicating that the variable doesn't point to any memory location. For e.g, int *ptr = NULL; The statement cout << ptr; would print 0. So, ptr is a NULL Pointer.

Pointer Arithmetic
A pointer variable stores an address which is just a numeric value. Thus we can perform arithmetic operations on it similar to the way we do on normal variables. There are four arithmetic operators that can be used on pointers ++, --, +, - . Following program demonstrate pointer arithmetic operations :

Suppose x is stored in memory location k i.e value stored in x is k. If we do x++, x now points to (k + 4) since at location k, an integer type variable is stored whose size is 4 bytes.

If you haven't realized yet, you have already learnt the basics of pointers. Now it's time to apply the fundamentals to solve problems. Well, pointer application will be introduced as and when required in the tutorial. We will end this topic by introducing one famous application of pointers.

Function Pointers
We have seen that pointer variable points to a memory location of another variable. A function pointer points to the memory location of an executable code i.e it can store the address of a function and can be used to invoke the function it points to. Using function pointers, we can select which function to invoke at run time depending on some values. In usual technique, functions to be invoked at particular portion of the program is decided at compile time. Following program demonstrates the use of function pointers :

We can observe that at run time, either the function add ( int, int ) or subtract ( int, int ) can de selected using the function pointer ( *operate )( int, int ). Also the statement operate = &add; can be written as operate = add; i.e ' & ' is optional here.