Tutorial Study Image

C++ Void Pointers


September 15, 2022, Learn eTutorial
1393

This tutorial mainly focuses on the void pointers in C++ and how to use them with the help of some examples.

Check C++ pointers before continuing with this tutorial.

Void Pointers in C++

  • The address of any data type can be stored in a void pointer which is a general-purpose pointer without a specific data type association.
  • A pointer with no associated data type is referred to as a void pointer.
  • A general-purpose pointer is known as a "void pointer" where it can typecast to any type as well as it can hold addresses of any type.
  • The fact that a pointer is marked as void does not necessarily mean it points to nothing. In reality, it is pointing, but we are unsure of the precise kind or type of object it is pointing to.

Syntax


void *ptr;  
 

The address of a variable with a particular data type cannot be assigned in C++ to a pointer with a different data type.

Let us consider an example :


int *ptr; //declaration of  integer pointer 

float a=20.2; // initialization of  floating variable 

ptr= &a;// An error is generated by this statement.

 

In the example above, we declare a float variable called "a" and a pointer of a type integer called "ptr." We attempt to store the address of a variable in ptr after the declaration, however, this is not possible in C++ because the variable cannot hold the address of distinct data types.

The address is a double type variable, in this case, that's why the error occurred.

The pointer, on the other hand, is of type int. In such circumstances, void pointers (pointers to void) in C++ can be used.

For Example,


void *ptr; // void pointer is used
                                                                      
double d = 20.2;                                                                      

ptr = &d; // hence it becomes a valid statement
 

The void pointer is a general pointer that is used when we don't know the data type of the variable that the pointer points to, as we discussed before in this article.

Example: Now let’s see how to use the void pointers in C++


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

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

    cout << "Address of floating point no will be: " << &f << endl;
    cout << "Address of void pointer no will be: " << ptr << endl;

    return 0;
}

Output:

Address of floating point no will be: 0x7ffca8253a6c
Address of void pointer no will be: 0x7ffca8253a6c

The value &f is here assigned to the pointer ptr.

The output demonstrates that a float variable named f address is stored in the void pointer ptr.

Since the void is an empty type, the void pointers cannot be dereferenced.


void* ptr;
float* fptr;
float f = 5.0;
                                                                      
// now assign  the float address to void pointer
ptr = &f;
cout << *ptr << endl;  // an error is occured
//now  assign the  float address to float pointer
fptr = &f;
cout << *fptr << endl;   //the  value is valid
 

Example 2: let us write a C++ program in order to print the content of a Void Pointer.

The static cast operation is mainly used in order to print a void pointer's contents. It changes the pointer's data type from the void* type to the appropriate data type for the address which the pointer is storing:

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

Output:

The Value of void pointer: 6.5

Program Explanation

  • The value of the address that the void pointer ptr points to is printed by this program.
  • We are unable to use *ptr because void pointers cannot be dereferenced.
  • The value pointed to by the void pointer can be used, though, if the void* pointer type is converted to the float* type.
  • In this illustration, the data type of the pointer was changed from void* to float* using the static_cast operator.

Example 3: let us write a program in  C++   in order to print the void pointer mainly using the typecasting.


#include 
using namespace std;
int main() {
    void* ptr;//  declaration of the void pointer
    int* ptr1;// declaration of the integer pointer
    int data = 7;//  initialization of the integer variable
  
    ptr = &data; // just store the address of data variable into the void pointer variable
    ptr1 = (int*)ptr; // assigning the void pointer to integer pointer
    
    cout << " Value of *ptr1 will be : " << *ptr1 << endl;
  
    return 0;
  }
 

Output:


Value of *ptr1 will be : 7

The explanation and working  of the above program :

  • We declare two pointer variables of types void and int, respectively, in the program shown above.
  • We also produce another variable of the integer type.
  • The address of the variable data is kept after the declaration in the void pointer variable ptr.
  • In order to assign the void pointer to an integer pointer, we must use the cast operator, ie, int, to the void pointer variable.
  • The void pointer's type is specified by the cast operator, which informs the compiler.
  • For casting, we must enter the data type and * in brackets, such as (char) or (int).

An important thing to remember

Addresses of variables with const or volatile qualifiers cannot be stored in void pointers.

For example


void *ptr;
const double d = 9.0;

// Error occurred:its because of the Invalid conversion from const void* to void*
ptr = &d;
 

What is the difference between a void pointer in C and a void pointer in C++?

In C, we can assign a void pointer to any other pointer type without typecasting it, however in C++, we must typecast when assigning a void pointer to any other pointer type.

Let us understand this concept with an example:

Pointer in C


#include <stdio.h>  
int main()  
{  
   void *ptr; // declaration of  void pointer 
   int *ptr1;  // declaration  integer pointer 
   int a =1000;  // initialization  of the integer variable
   ptr=&a; //     storing the address of 'a' in ptr  
   ptr1=ptr; // assigning the void pointer to  integer pointer type.  
   printf("value of *ptr1 will be: %d",*ptr1);  
   return 0;  
}

Output:

value of *ptr1 will be: 1000

Two pointers of types void and integer are declared in the program above as "ptr" and "ptr1," respectively. Additionally, we declare the integer type variable "a." After declaration, we provide the pointer "ptr" the address of the variable "a." Then, as typecasting is not required in C when assigning the void pointer to any other type of pointer, we assign the void pointer to the integer pointer, i.e., ptr1, directly.

Pointer in C++


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

Output:

The value of *ptr1 is will be : 50

Two pointer variables of the types void and int are declared in the programme above. We also created a new "data" which is a integer type variable. After declaration, the address of the variable "data" is kept in the void pointer variable "ptr." So now we wish to assign the void pointer to an integer pointer, we must use the cast operator on the void pointer variable, which is represented by (int *). The void pointer's type is specified by the cast operator, which informs the compiler. To cast, enter the data type as well as * in a bracket, such as (char *) or (int *).