Pointers are powerful features of C++ that enables you to manipulate the data in the computer’s memory directly. A pointer is container, but instead of storing value, pointer stores an address so a memory location.
Ex:
1 2 3 4 5 6 7 8 9 | #include<iostream> using namespace std; int main() { int n = 5; cout << n << endl <<&n; return 0; } |
Output:
1 2 | 5 // value inside variable 006FFD64 // address of a variable |
Syntax:
- ptr is the name of the pointer.
- datatype is the type of data it is pointing to.
Ex:
1 | *ptr; // ptr can point to an address which holds int data |
How to Use Pointers?
The use of pointers in C can be divided into three steps:
- Pointer Declaration
- Pointer Initialization
- Pointer Dereferencing
Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it. To declare a pointer, we use the ( * ) dereference operator before its name.
Ex:
1 | int *ptr; |
The pointer declared here will point to some random memory address as it is not initialized. Such pointers are called wild pointers.
Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer variable. We generally use the ( & ) addressof operator to get the memory address of a variable and then store it in the pointer variable.
Ex:
1 2 3 | int var = 10; int * ptr; ptr = &var; |
We can also declare and initialize the pointer in a single step. This method is called pointer definition as the pointer is declared and initialized at the same time.
Ex:
1 | int *ptr = &var; |
Note:
It is recommended that the pointers should always be initialized to some value before starting using it. Otherwise, it may lead to number of errors.
Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in the memory address specified in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer declaration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> void wrap() { int var = 10; // declare pointer variable int* ptr; // note that data type of ptr and var must be same ptr = &var; // assign the address of a variable to a pointer printf("Value at ptr = %p \n", ptr); printf("Value at var = %d \n", var); printf("Value at *ptr = %d \n", *ptr); } // Driver program int main() { wrap(); return 0; } |
Output:
1 2 3 | Value at ptr = 0x7fff1038675c Value at var = 10 Value at *ptr = 10 |
Types of Pointers
Pointers in C can be classified into many different types based on the parameter on which we are defining their types. If we consider the type of variable stored in the memory location pointed by the pointer, then the pointers can be classified into the following types:
Integer Pointers
As the name suggests, these are the pointers that point to the integer values.
Syntax:
These pointers are pronounced as Pointer to Integer.
Similarly, a pointer can point to any primitive data type. It can point also point to derived data types such as arrays and user-defined data types such as structures.
Array Pointer
Pointers and Array are closely related to each other. Even the array name is the pointer to its first element. They are also known as Pointer to Arrays. We can create a pointer to an array using the given syntax.
Syntax:
Structure Pointer
The pointer pointing to the structure type is called Structure Pointer or Pointer to Structure. It can be declared in the same way as we declare the other primitive data types.
Syntax:
Function Pointers
Function pointers point to the functions. They are different from the rest of the pointers in the sense that instead of pointing to the data, they point to the code. Let’s consider a function prototype – int func (int, char), the function pointer for this function will be:
Syntax:
Note:The syntax of the function pointers changes according to the function prototype.
Double Pointers
We can define a pointer that stores the memory address of another pointer. Such pointers are called double-pointers or pointers-to-pointer. Instead of pointing to a data value, they point to another pointer.
Syntax:
*pointer_name; // get the address stored in the inner level pointer
**pointer_name; // get the value pointed by inner level pointer
NULL Pointer
The Null Pointers are those pointers that do not point to any memory location. They can be created by assigning a NULL value to the pointer. A pointer of any type can be assigned the NULL value.
Syntax:
data_type *pointer_name = NULL;
or
pointer_name = NULL
It is said to be good practice to assign NULL to the pointers currently not in use.
Void Pointer
The Void pointers are the pointers of type void. It means that they do not have any associated data type. They are also called generic pointers as they can point to any type and can be typecasted to any type.
Syntax:
Wild Pointers
The Wild Pointers are pointers that have not been initialized with something yet.
Ex:
1 2 | int *ptr; char *str; |
Constant Pointers
In constant pointers, the memory address stored inside the pointer is constant and cannot be modified once it is defined. It will always point to the same memory address.
Syntax:
Pointer to Constant
The pointers pointing to a constant value that cannot be modified are called pointers to a constant. Here we can only access the data pointed by the pointer, but cannot modify it. Although, we can change the address stored in the pointer to constant.
Syntax: