What is the Function of a Pointer in C and C++?
A pointer in C and C++ allows direct memory manipulation, dynamic memory allocation, and implementation of data structures like linked lists and trees.
When we create a pointer variable, we specify the data type it will point to. For example, we can create an integer pointer or a character pointer. This is called a declaration of a pointer variable.
A pointer can point to any data type. A pointer can be re-assigned any number of times. You can get the value of a pointer by preceding its name with the * (dereference) operator. For example, if ptr is a pointer to an integer, then *ptr is the value pointed to by ptr. The & and * operators are complements of each other. &*ptr gives the address of ptr, and *&ptr gives the value pointed to by ptr.
As the name suggests, a pointer is used to point to a particular object. In the programming world, the concept is the same, here, a pointer is used to store the address or memory location of a variable.
Example: int a = 5;
int *ptr = a;
But Why Use Pointers?
First, pointers can be used to allocate memory dynamically. That is, to allocate memory at runtime rather than compile time. This can be useful when we don’t know how much memory our program will need ahead of time.
Second, pointers can be used to pass arguments by reference. That is, to pass the address of an argument rather than passing the argument by value. This can be used to avoid copying large amounts of data.
Finally, pointers can create data structures, such as linked lists and trees.
When used properly, pointers can be a powerful tool. But they can also be dangerous. Improper use of pointers can lead to memory leaks and other problems.
So, it’s important to understand how pointers work before using them in your own programs.
Well, it can be understood through a real-life example. Consider a courier is to be delivered to a place, then the address is the best and fastest delivery method. Similarly, there is a large number of memory locations, and to access any part of it, you need to have an address for that location.
A pointer is represented by (*) and an address is represented by &. To access the value stored in the address we use the unary operator (*) that returns the value of the variable located at the address specified by its operand. This is also called Dereferencing. Pointers can be used with 1D arrays and 2D arrays. A pointer can point to another pointer also.
#include<iostream>
using namespace std;
int main()
{
int a = 5;
cout<< "Variable a has value" << a << endl;
int *ptr = &a;
int **p = &ptr; // pointer of a pointer
cout<<"Address of that location is" << &a << endl;
cout<<"Value of the pointer ptr is " << ptr << endl;
cout<<"Value of the pointer p is " << p << endl;
return 0;
}
Output :
Variable a has a value 5
The address of that location is 0x6ffe34
The value of the pointer ptr is 0x6ffe34
Expressions and Arithmetic operations on pointer
You can perform the following operations on the pointer:
- Increment (++)
- Decrement (–)
- Subtraction (-=)
- Addition (+=)
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Declare an array
int v[5] = {10, 100, 200, 300, 400};
// Declare pointer variable
int *ptr;
// Assign the address of v[0] to ptr
ptr = v;
for (int i = 0; i < 5; i++)
{
printf("Value of *ptr = %dn", *ptr);
printf("Value of ptr = %pnn", ptr);
// Increment pointer ptr by 1
ptr++;
}
}
Summary
In conclusion, pointers in C and C++ are powerful tools that allow for efficient memory management, direct access to memory addresses, and the ability to create complex data structures. Understanding how to work with pointers is essential for more advanced programming tasks and optimising performance in these languages.