In this lecture, we will explain what pointers are and why they are used in C++. We will explain everything with practical examples.
A pointer in C++ is like a signpost. A pointer is a variable that stores the address of another variable. When we declare a variable like,
int num = 11;
We declare a variable, internally it allocates a memory on the stack, and assigns the given value to that memory. Here, num
is a variable holding an integer value, 11.
Whereas, a pointer is a special type of variable that doesn’t store the value directly. Instead, it holds the address if memory location of another variable. Like this,
int * ptr = #
We use the ampersand (&) symbol to get the memory location from a variable num
, and then store this memory address in pointer ptr
.
If we can access a variable directly, then why do we need a pointer in C++? There are many benefits of Pointers in C++, let’s discuss them one by one,
In C++, with the help of pointers, we can perform dynamic memory allocation and deallocation. It means, we can allocate memory at runtime using the ‘new’ operator, and also we can free the memory using ‘delete’ operator. So, basically pointers give more flexibility in management of memory in C++.
We can use the pointer to traverse through the array elements efficiently, by performing the increment & decrement operations on pointer, instead of using array index to access elements.
We can use pointers to pass variables to a function without creating an extra copy of variable. This way functions can operate on on the original data directly, without creating a copy of it.
Data Structures and Object-Oriented Programming:
Pointers are essential in implementing data structures like trees and linked lists. Also, they are extremely useful in Object-Oriented Programming.
The syntax for declaring a pointer in C++ is as follows:
DataType * VariableName;
DataType
is the type of the variable or value that memory holds and pointer will point to that memory location.*
signifies that the variable is a pointer.VariableName
is the name of the pointer variable.Let’s see an integer pointer as an example:
int num = 16; // Here, 'ptr' is a pointer to an integer. int * ptr = #
This pointer ptr
doesn’t hold an integer but a memory address where an integer ‘num’ is stored.
In the above code, variable num
is an integer that holds the value 16
. The &
operator is used to get the address of variable num
. Now, ptr
contains the memory address of variable num
, so ptr
is an integer pointing to mempry of variable num
.
As we mentioned in the syntax, the datatype of a pointer depends on the data type of variable whose addess this pointer is going to store.
Let’s see some examples of different type of pointers.
An integer pointer stores the memory address of an integer. Here is an example of an integer pointer:
#include <iostream> int main() { // An integer variable. int num = 16; // An integer pointer which contains the address of num int * ptr = # std::cout<< "Value stored in variable num: " << num <<std::endl; std::cout<< "Address of variable num: " << ptr <<std::endl; return 0; }
Output:
Value stored in variable num: 16 Address of variable num: 0x7fff7a25d98c
A character pointer stores the memory address of a character. Here is an example of a character pointer:
#include <iostream> int main() { // An character variable. char ch = 'A'; // An character pointer which contains the address of ch char * ptr = &ch; std::cout<< "Value stored in variable ch: " << ch <<std::endl; std::cout<< "Address of variable ch: " << ptr <<std::endl; return 0; }
Output:
Value stored in variable ch: A Address of variable ch: 0x67676782
A double pointer stores the memory location of a double. Here is an example of a double pointer:
#include <iostream> int main() { // A double variable. double num = 78.8; // An pointer pointer which contains the address of num double * ptr = # std::cout<< "Value stored in variable num: " << num <<std::endl; std::cout<< "Address of double ch: " << ptr <<std::endl; return 0; }
Output:
Value stored in variable num: 78.8 Address of double ch: 0x7ffc476b6768
Pointers are an essential part of C++. It helps in optimising memory use, improving performance, and providing a base for more complex data structures. So, if you planning to enhance your programming skills in C++, then learning Pointers is must for you.