Application of Pointer in C++
In C++, 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.
1. Pass Arguments by Pointer
Pointers are commonly used to pass arguments by pointers to functions. Passing arguments by pointers allows a function to modify the original variables passed to it. Instead of passing a copy of the variable, the function receives the address of the original variable. This means any changes made to the parameter within the function will directly affect the original variable outside of the function.
Example:
#include <iostream>
using namespace std;
// Function that takes pointer to integer
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. For 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. 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:
5. 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!
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.
Example:
#include <iostream>
using namespace std;
// Base class
class Base {
public:
// virtual function
virtual void display() { cout << "Base display\n"; }
void print() { cout << "Base print\n"; }
};
// Child class
class Child : public Base {
public:
void display() override { cout << "Child display\n"; }
void print() { cout << "Child print\n"; }
};
int main() {
// Create a pointer of type Base
Base* basePtr;
// Point basePtr to an object of type Child
Child childObj;
basePtr = &childObj;
// Call the virtual function
basePtr->display();
// Call the non-virtual function
basePtr->print();
return 0;
}
Output
Child display Base print