Loading...
C++ Void Pointers

C++ Void Pointers

In this tutorial, we will learn about void pointers and how to use them with the help of examples.

Before you proceed with this tutorial, be sure to check C++ pointers.

Void Pointer

  • A void pointer is A pointer that has no associated data type with it.
  • A void pointer is a general purpose pointer that can hold address of any type and can be typecasted to any type.
  • The void pointer doesn't mean it points to nothing. Actually, it is pointing something but we just don't know the exact type of object it is pointing to.

Syntax

void *ptr;

In C++, we cannot assign the address of a variable of one data type to a pointer of another data type.

Consider this example:

// pointer is of int type
int *ptr;

// variable is of double type
double d = 2.0;

// Error occurs
// we can't assign double* to int*
ptr = &d;

Here, the error occurred because the address is a double type variable. However, the pointer is of int type.

In such situations, we can use the void pointers (pointer to void) in C++.

For example:

// void pointer
void *ptr;
                                                                      
double d = 2.0;
                                                                      
// valid statement
ptr = &d;

We discuss earlier in this tutorial, The void pointer is a generic pointer that is used when we don't know the data type of the variable that the pointer points to.


Example 1: C++ Program how to use Void Pointer

#include <iostream>
using namespace std;
int main() {
    void* ptr;
    float f = 2.0f;

    // assign float address to void
    ptr = &f;

    cout << "The address of floating point no.: " << &f << endl;
    cout << "The address of void pointer no.: " << ptr << endl;

    return 0;
}

Output

The address of floating point no.: 0x7ffe1a5a540c
The address of void pointer no.: 0x7ffe1a5a540c

Here, the pointer ptr is given the value of &f.

The output shows that the void pointer ptr stores the address of a float variable f.


As void is an empty type, void pointers cannot be dereferenced.

void* ptr;
float* fptr;
float f = 2.0;
                                                                      
// assign float address to void pointer
ptr = &f;
cout << *ptr << endl;  // Error Occurs

// assign float address to float pointer
fptr = &f;
cout << *fptr << endl;   // Valid value

Example 2: Printing the Content of Void Pointer

To print the content of a void pointer, we use the static_cast operator. It converts the pointer from void* type to the respective data type of the address the pointer is storing:

#include <iostream>
using namespace std;
int main() {
    void* ptr;
    float f = 2.0f;
  
    // assign float address to void pointer
    ptr = &f;
  
    cout << "The Value of void pointer: ";
    // use type casting to print pointer content
    cout << *(static_cast <float*>(ptr));
  
    return 0;
  }

Output

The value of void pointer: 2.0

This program prints the value of the address pointed to by the void pointer ptr.

Since we cannot dereference a void pointer, we cannot use *ptr.

However, if we convert the void* pointer type to the float* type, we can use the value pointed to by the void pointer.

In this example, we have used the static_cast operator to convert the data type of the pointer from void* to float*.


Example 3: Print the Void Pointer using type casting

#include <iostream>
using namespace std;
int main() {
    void* ptr;// void pointer declaration
    int* ptr1;// integer pointer declaration
    int data = 2;// integer variable initialization
  
    ptr = &data; // storing the address of data variable in void pointer variable
    ptr1 = (int*)ptr; // assigning void pointer to integer pointer
    
    cout << "The Value of *ptr1: " << *ptr1 << endl;
  
    return 0;
  }

Output

The value of *ptr1: 2

Working

  • In this above program, we declare two pointer variables of type void and int type respectively.
  • We also create another interger type variable data.
  • After declaration, we store the address of variable data in a void pointer variable ptr.
  • Now, we want to assign the void pointer to integer pointer, in order to do this, we need to apply the cast ooperator ie,. int to the void pointer variable.
  • This cast operator tells the compiler which type of value void pointer is holding.
  • For casting, we have to type the data type and * in a bracket like (char) or (int).

Note: void pointers cannot be used to store addresses of variables with const or volatile qualifiers.

void *ptr;
const double d = 2.0;

// Error occurs: Invalid conversion from const void* to void*
ptr = &d;

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Void Pointers in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Pointer & Arrays.

- Related Topics