C++ Pointers

In this tutorial, you will learn about one of the most important concepts of C++ i.e. pointers in C++ with the help of examples.


Before understanding what pointers are, we should first understand how various variables are stored in computer memory. Consider that the below figure is of the memory of a computer. The memory of the computer in the context of program execution is assumed to be Random Access Memory(RAM). Suppose that each segment in the memory is equal to one byte of memory. As we know that, in a typical computer architecture each byte of memory has an address associated with it. If we declare a variable of type int in our program such as –

int a;
Structure of Memory
Structure of Memory

so, when this program executes, the computer will allocate some amount of memory to this specific variable. The amount of memory allocated to the variable depends upon the data type and compiler. In a modern-day compiler, an integer data type is allocated four bytes of memory. As soon as the compiler spots this declaration during program execution, it will allocate 4 bytes of memory. In our example, it allocates memory to variable ‘a’ starting from address 204 to 207. All of this information is stored in an internal structure called a lookup table. It makes an entry inside the lookup table that variable ‘a’ of type int is located at address 204, which is the starting address of the variable. Now, if we initialize the variable ‘a’ with a value such as-

int a = 5;

then, the compiler will search for this variable in the lookup table. It will find that the variable is located at address 204 to 207, so it will write the value = 5 in binary form in that address.

C++ Pointers

Pointers are simply variables that store the address of other variables. Let us take an example to understand pointers-

Suppose that we have a block of 4 bytes of memory that stores the variable ‘a’ of int type at address 204. Now, we will define another variable ‘p’ whose type is a pointer to an integer which also takes 4 bytes of memory and it is located at address 104. This variable ‘p’ can store the address of variable ‘a’. Upon applying some operators on variable ‘p’, we can point to variable ‘a’.

C++ Pointer
C++ Pointer Example

The syntax of a pointer is –

datatype * p;

Normally, we declare a variable by writing the data type along with the variable name. While declaring a pointer, we write data type, variable name and add an asterisk(*) symbol before the variable name.

int a; //declaring a variable ‘a’.
int * p; // declaring a pointer ‘p’ that will store the address of variable ‘a’.

To store the address of variable ‘a’ in ‘p’, we use an ampersand(&) symbol –

p = &a;

Advantages of Pointers

Pointers are boon for programmers, and the major reasons behind this statement are as follows –

  • Pointers make your code shorter and hence making the size of your code lesser and easier to understand.
  • Pointers help in debugging the code faster.
  • Pointers make dealing with unique addresses easier.
  • They are also very useful when you want to enhance the processing speed.

Usage of Pointers

Pointers offer a wide range of variety for the programmers, and some of them are mentioned below –

  • Dynamic Memory Allocation- Sometimes, the memory allocated at the run is not sufficient, and when we manually change the memory at run time, it is stated as Dynamic Memory Allocation. It is done using malloc() and calloc() functions that won’t be used if pointers are not used.
  • Arrays, Functions, and Structures – You will find pointers being extensively used while dealing with Arrays, functions, and structures. They make the code shorter and easier to debug.

How to obtain the value from the address using a pointer variable?

We will use another piece of code to understand this concept.

int* pointerXYZ, abc;
abc = 5;

// assign address of var to pointVar
pointerXYZ = &abc;

// access value pointed by pointVar
cout << *pointerXYZ << endl; // Output: 5

Here, initially, we will assign the address of abc variable into the pointer variable pointerXYZ. Now in order to print the value, we will be required to use *(asterisk sign) with the pointer variable, and it is commonly known as the dereference operator. So *pointerXYZ will print five as the output.

C++ Pointer example

Now since you must be familiar with the basics, let’s look at a simple example depicting the working of pointers in C++. Here is the piece of code for the same.

#include <iostream>
using namespace std;
int main(){
//Pointer declaration
int *p, var=101;

//Assignment
p = &var;

cout<<"Address of var: "<<&var<<endl;
cout<<"Address of var: "<<p<<endl;
cout<<"Address of p: "<<&p<<endl;
cout<<"Value of var: "<<*p;
return 0;
}

Output

Address of var: 0x61feb8
Address of var: 0x61feb8
Address of p: 0x61febc
Value of var: 101

We hope this article has helped you to understand the concept of pointers in C++.

Please get connected & share!

Advertisement