In C++, a pointer is a variable that stores the memory address as its value. It is a powerful feature that forms the backbone of lower-level memory manipulation in C++. It is used in a variety of applications. Some of the common applications of pointers are discussed in this article.
Some of the common applications of pointers
Some of the common applications of pointers in C++ are discussed below to understand their practical usage in real-world programming. let's discuss one by one
1. Pass Arguments by References
Passing arguments using pointers allows functions to modify the original variables by accessing their memory addresses.
Example:
#include <iostream>
using namespace std;
void modifyValue(int* ptr) {
(*ptr) += 5;
}
int main() {
int x = 10;
cout << "Original: " << x << endl;
// Passing address of 'x' to modifyValue()
modifyValue(&x);
cout << "Modified: " << x;
return 0;
}
Output
Original: 10 Modified: 15
2.Dynamic Memory Allocation
In C++, dynamic memory allocation is very important application of pointer that allows programs to request and manage memory during runtime. Pointers are essential when allocating memory at runtime. They are used to store the address of allocated memory using new or malloc().
Example:
#include <iostream>
using namespace std;
int main() {
// Dynamically allocate memory for an integer
int* ptr = new int(5);
cout << "Value: " << *ptr;
// Free the dynamically allocated memory
delete ptr;
return 0;
}
Output
Value: 5
3. Array and Pointer Arithmetic
Pointers and arrays are closely related. The name of an array in C++ is essentially a constant pointer to its first element. This allows us to manipulate array elements using pointer arithmetic.
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
// Pointer to first element of array
int* ptr = arr;
for (int i = 0; i < 5; ++i) {
// Access array elements using pointer arithmetic
cout << *(ptr + i) << " ";
}
return 0;
}
Output
1 2 3 4 5
Explanation: Here, ptr points to the first element of the array. Using pointer arithmetic, we access and print the elements of the array by incrementing the pointer.
4. Function Pointers
In C++, function pointers are used to store the address of a function and allow dynamic function calls. They can be used to implement callback functions, event handlers, and more.
Example:
#include <iostream>
using namespace std;
void greet() {
cout << "Hello, world!";
}
int main() {
// Declare function pointer and point it to greet
void (*func_ptr)() = &greet;
// Call function using pointer
func_ptr();
return 0;
}
Output
Hello, world!
5. Implementing Data Structures
Pointers are used for creating and managing dynamic data structures such as linked lists, trees, and graphs in C++. By using pointers, elements can be linked together dynamically during runtime, allowing efficient memory use.
Example:
6. Achieving Runtime Polymorphism
Function pointers are used to achieve runtime polymorphism in C++. Compiler internally create a pointer to the base class and based on the object it is pointing to; it finds the function to be executed. Not only that, but pointers are also used by the compiler to keep the track of virtual functions in the class.