C++ Declaration and Initialization of Pointers

Pointer variables are declared like normal variables except for the addition of the unary ∗ character.

C++ Pointer Declaration

The general form of a pointer declaration is as follows :

type ∗var_name ;

where type is any valid C++ data type and var_name is the name of the pointer variable. Following declarations declares pointers of different types :

int ∗iptr ;      //creates an integer pointer iptr
char ∗cptr ;      //creates a character pointer cptr
float ∗fptr ;      //creates a float pointer fptr

When we say that iptr is an integer pointer, it means that the memory location being pointer to by iptr can hold only integer values. In other words, it can stated that iptr is a pointer to integer. Similarly, in the above given declarations, cptr is a pointer to a character and fptr is a pointer to a floating-point value. Thus, it is the base type of the pointer that defines what types of variables the pointer can point to. Technically, any type of pointer can point anywhere in memory.

However, all pointer arithmetic is done relative to its base type, so it is important to declare the pointers correctly.

Two special operators ∗ and & are used with pointers. The & is a unary operator that returns the memory address of its operand. For example,

int i = 25 ;     // declares an int variable i
int ∗iptr ;      // declares an int pointer iptr
iptr = &i ;       // stores the memory address of i into iptr

The above given assignment statement stores the memory address of i (& returns memory address of its operand) into iptr so that iptr is now pointing to the memory location of i. The expression &i will return an int pointer value because i is an integer thus, &i will return a pointer to integer. Similarly, if ch is a character variable, then &ch will return a char pointer value.

Note - The operand of & operator is as ordinary available but operand of ∗ is a pointer variable.

Using '∗' operator, changing/accessing value being pointed to by pointer (its state) is called Dereferencing.

Dereferencing refers to changing/accessing state of the pointer.

The pointer operator ∗ (called "at address" sign) is the same sign as multiply operator ∗ and the pointer operator & ( called "address of" sign) is same as bitwise AND operator &. But these operators have no relationship to each other, Both pointer operator & and ∗ have a higher precedence than all other arithmetic operators except the unary minus, with which they have equal precedence.

C++ Pointers Initialization

Attention - A pointer variable must not remain uninitialized since uninitialized pointers cause the system crash. Even if you do not have any legal pointer value to initialize a pointer, you can initialize it with NULL pointer value.

Assigning NULL to a pointer initializes it to a value which is not a legal pointer but it saves you from the 'Big' problem of uninitialized pointers. A program can later test a pointer against NULL (the zero pointer) to see whether it contains a legal pointer or not. For example, consider the following code fragment :

int ∗iptr = NULL ;      //iptr gets initialized but does not to a legal address
if(iptr != NULL)
   :      //use the pointer if it pointers to a legal address

The expression (iptr != NULL) is equivalent to expression (iptr).

The zero pointer NULL is defined in the standard file stddef.h.

When you use NULL for initializing pointers then before using the pointer anywhere in the program, first check that it is non-zero or non-NULL as shown in the above code fragment.

nullptr in C++

In C++, a new keyword namely nullptr has been introduced, which is a distinguished null-point constant. So, now on (provided compiler implements C++ standard), you can initialize your pointers with nullptr, e.g.,

char ∗cptr = nullptr ;
int ∗iptr = nullptr ;

Difference between NULL and nullptr

Difference between NULL and nullptr are:

  • NULL is not defined by C++, it is programmer or compiler-defined whereas nullptr has been defined by C++ as a legal null pointer
  • NULL is internally mapped to int 0(zero) whereas nullptr is a legal empty/null pointer, which is implicitly convertible and comparable to any pointer-type. This feature has another benefit too. Until now if you had two overloaded functions.

Here are examples demonstrating the difference between NULL and nullptr

int f1(char∗) ;
int f1(int) ;

And if you have a pointer as

char ∗cptr = NULL ;

then a function call f1(cptr) would always map to f1(int) and not to f1(char∗) because internally NULL maps to an int. But with nullptr i.e.,

char ∗cptr = nullptr ;

function call f1(cptr) will map to f1(char∗) only.

C++ Pointer Arithmetic

Only two arithmetic operation, addition and subtraction, may be performed on pointers. When you add 1 to a pointer, you are actually adding the size of whatever the pointer is pointing at. That is, each time a pointer is incremented by 1, it points to the memory location of the next element of its base type. To understand it more clearly, let us take an example. Let iptr be an integer pointer, currently pointing to memory address 1001. Assuming that the int size is 2 bytes, then after the following expression

iptr++ ;

iptr will be pointing to 1003, not to 1002. Each time you increment iptr by 1, it will point to the next integer. Similarly, when you decrement iptr by 1 it will point to the location of the previous element of its base type (int here) which is at address 999.

When a character pointer is incremented, its value increases by one or what ever is size of a char on your machine; an integer pointer when incremented, its value increases by two (or whatever would be the size of int on your machine); a float pointer when incremented, its value increases by eight (or whatever would be the size of float on your machine), and so forth.

Base Address - A pointer holds the address of the very first byte of the memory location where it is pointing to. The address of the first byte is known as Base Address.

C++ Online Test

« Previous Tutorial Next Tutorial »