Introduction to C++ | Void Pointer

C++ Void Pointer

A void pointer in C++ is a pointer that can point to any data type. It is a generic pointer type that does not have a specific data type associated with it. This flexibility makes it useful in functions or data structures where the type of data is not known in advance.

What is a Void Pointer?

A void pointer (also known as a generic pointer) is a pointer that can hold the address of any data type but does not have any specific data type associated with it. It is declared using the void keyword and can be used to store the address of variables of any type (e.g., int, char, float, etc.).

Syntax:

Syntax of Void Pointer


        void* pointerName;
                    

Example of Void Pointer

Here’s an example of how to declare and use a void pointer:

Code Example: Void Pointer


        #include <iostream>
        using namespace std;
        
        int main() {
            int a = 10;
            float b = 5.5;
            
            // Declare a void pointer
            void* ptr;
        
            // Assign the address of an integer variable to the void pointer
            ptr = &a;
            cout << "Value of a: " << *static_cast<int*>(ptr) << endl;  // Casting void pointer to int pointer
            
            // Assign the address of a float variable to the void pointer
            ptr = &b;
            cout << "Value of b: " << *static_cast<float*>(ptr) << endl;  // Casting void pointer to float pointer
            
            return 0;
        }
                    

Output

Value of a: 10
Value of b: 5.5

Why Use Void Pointers?

Void pointers are often used in situations where you need a generic pointer that can point to any data type. Common use cases include:

Type Casting with Void Pointers

Since a void pointer does not have a specific type, you must cast it to the appropriate pointer type before dereferencing it. In C++, the static_cast operator is commonly used for this purpose.

Example of type casting with void pointers:

Code Example: Type Casting with Void Pointer


        #include <iostream>
        using namespace std;
        
        int main() {
            int a = 10;
            char ch = 'A';
            
            // Declare a void pointer
            void* ptr;
        
            // Assign address of an int variable
            ptr = &a;
            cout << "Value of a: " << *static_cast<int*>(ptr) << endl;
        
            // Assign address of a char variable
            ptr = &ch;
            cout << "Value of ch: " << *static_cast<char*>(ptr) << endl;
            
            return 0;
        }
                    

Output

Value of a: 10
Value of ch: A

Limitations of Void Pointers

While void pointers are very flexible, they come with some limitations:

Example: Void Pointer in a Function

Void pointers can also be used in functions that need to handle different types of data. For example, a function that accepts a void pointer as a parameter can work with any data type.

Code Example: Void Pointer in a Function


        #include <iostream>
        using namespace std;
        
        // Function that takes a void pointer
        void printValue(void* ptr, char type) {
            if (type == 'i') {
                cout << "Integer value: " << *static_cast<int*>(ptr) << endl;
            }
            else if (type == 'f') {
                cout << "Float value: " << *static_cast<float*>(ptr) << endl;
            }
        }
        
        int main() {
            int a = 10;
            float b = 5.5;
        
            // Calling function with void pointer
            printValue(&a, 'i');
            printValue(&b, 'f');
            
            return 0;
        }
                    

Output

Integer value: 10
Float value: 5.5

Conclusion

A void pointer is a powerful feature in C++ that allows you to write generic functions and data structures. It can store the address of any data type, providing flexibility. However, you must cast the void pointer to the correct type before dereferencing it. While it is versatile, it requires caution, as incorrect casting can lead to errors or undefined behavior.

Pro Tip:

💡 Pro Tip

Use void pointers when you need a function or data structure to be flexible with data types. However, always ensure the correct type is cast before dereferencing to avoid errors.