In C++, a void pointer is a pointer that is declared using the 'void' keyword (void*). It is different from regular pointers it is used to point to data of no specified data type. It can point to any type of data so it is also called a "Generic Pointer".
Syntax of Void Pointer in C++
void* ptr_name;
As the type of data the void pointer is pointing to is unknown, we cannot dereference a void pointer.
Example of Void Pointer in C++
The below example demonstrates the declaration and use of void pointer in C++.
C++
// C++ Program to demonstrate the declaration and use of a
// void pointer
#include <iostream>
using namespace std;
int main()
{
int a = 10;
// void pointer holds address of int 'a'
void* myptr = &a;
// printing the value of a and adress of a stored in
// myptr
cout << "The value of a is: " << a << endl;
cout << "The Adress of a is " << myptr << endl;
}
OutputThe value of a is: 10
The Adress of a is 0x7ffd9ac02a64
Application of Void Pointer in C++
1. Generic Coding
The concept of a generic pointer means a pointer that can be used to point to data of any data type. This is helpful in situations where you need a single pointer that can handle different data types dynamically.
Let's see an example for this, let's first declare variables of different types (int, double, char). then declare a void pointer named "generic pointer". We use the void pointer to point to each of the variables in succession with the help of typecasting.
Example
Below is a program in which an int type pointer is declared and we try to point the value of a float datatype using it. Let's See what happens.
C++
// C++ program to demonstrate the need for void pointer
#include <iostream>
using namespace std;
int main()
{
int* ptr;
float f = 90.6;
ptr = &f; // error
cout << "The value of *ptr is : " << *ptr << endl;
return 0;
}
Output
error: cannot convert ‘float*’ to ‘int*’ in assignment
Explanation: The above program gives an error the reason is int type pointer ptr is declared and we are trying to store the memory address of a float data type into an int pointer, which gives an error. But if we declare void pointer it allows us to change their data type. So, declare ptr as a void pointer and the program will not give any error. The below example demonstrates the same.
C++
// C++ Program to demonstrate the use of void pointer
// to hold the address of any type-castable type
#include <iostream>
using namespace std;
int main()
{
// Initializing multiple variables of different data
// type
int n = 10;
float f = 25.25;
char c = '$';
// Initializing a void pointer
void* ptr;
ptr = &n; // pointing to int
cout << "The value of n " << n << endl;
cout << "The Adress of n " << ptr << endl;
ptr = &f; // pointing to float
cout << "The value of n " << f << endl;
cout << "The Adress of n " << ptr << endl;
ptr = &c; // pointing to char
cout << "The value of n " << c << endl;
cout << "The Adress of n " << ptr << endl;
}
OutputThe value of n 10
The Adress of n 0x7ffe05023434
The value of n 25.25
The Adress of n 0x7ffe05023430
The value of n $
The Adress of n 0x7ffe0502342f
Explanation: In the above Code, We declared a void pointer as the variable name "ptr" which acts as a generic pointer or void pointer. This pointer stores multiple datatype addresses like int, char and float. We Typecasted the void pointer to any data type (float, char, int). Hence Void Pointer allows us to change the addresses for different memory addresses of different data types.
2. Dynamic Memory Allocation
Dynamic memory allocation is performed in C++, when the size of memory needed is not known at compile time then we perform Dynamic memory Allocation using operators like new or malloc. Void pointers can be used to allocate memory for any data type. In C++, "new" keyword is used for dynamic memory allocation which returns a pointer to the allocated memory. After allocating memory, We need perform type casting to use the allocated memory with a specific data type.
Example
The below program demonstrate the use of void pointer to dynamically allocate memory for any data type.
C++
// C++ program to demonstrate the use of void pointer to
// dynamically allocate memory for any data type.
#include <iostream>
using namespace std;
int main()
{
// Allocate memory for an integer using new
void* voidPtr = new int;
// Type casting the void pointer to int* for usage
int* intPtr = static_cast<int*>(voidPtr);
// Assign a value to the allocated memory
*intPtr = 42;
// Print the value from the allocated memory
cout << "Value from allocated memory: " << *intPtr
<< endl;
// Deallocate the memory
delete intPtr;
return 0;
}
OutputValue from allocated memory: 42
3. Callback Functions
Functions that are passed as arguments to other functions are called callback functions. In case we need to handle multiple data types, the void pointer can be used as a void pointer provides us the flexibility to handle different data types. Callback functions can be defined to accept a void pointer as a parameter, enabling them to handle different types of data. This approach simplifies the code because this approach maintains different data types in a single callback.
Example
The below program demonstrates the use of the void function to pass a void pointer as a parameter in a callback function.
C++
// The below program demonstrate the use of void function to
// pass void pointer as parameter in callback function.
#include <iostream>
using namespace std;
// Callback function definition
void CallbackFunction(void* data, char dataType)
{
switch (dataType) {
case 'i':
cout << "Callback for integer: " << *(int*)data
<< endl;
break;
case 'd':
cout << "Callback for double: " << *(double*)data
<< endl;
break;
default:
cout << "Unsupported data type in callback!"
<< endl;
}
}
// Function that takes a callback
void PerformOperation(void* data, char dataType,
void (*callback)(void*, char))
{
// Call the callback function
callback(data, dataType);
}
int main()
{
int intValue = 07;
double doubleValue = 8.12;
// Perform operation with integer and callback
PerformOperation(&intValue, 'i', CallbackFunction);
// Perform operation with double and callback
PerformOperation(&doubleValue, 'd', CallbackFunction);
return 0;
}
OutputCallback for integer: 7
Callback for double: 8.12
Advantages of void Pointer in C++
The void pointer have the following major applications:
- The use of void pointers can help reduce redundant code by allowing a single piece of code to handle multiple data types.
- Void pointer allows abstraction of specific types in favor of a more general interface. This is common in scenarios like polymorphic containers or callback systems.
- A void pointer can be declared to deal with polymorphism.
- Void pointers can enhance platform independence where the sizes and representations of data types may vary across different architectures.
Conclusion
In conclusion, void pointers in C++ offer flexibility and versatility in certain programming scenarios, but this demands careful consideration. Modern C++ offers safer alternatives, like smart pointers for memory management.
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read