C++:Pointers|Void Pointer

C++ Void Pointer

A void pointer is a general-purpose pointer that can hold the address of any data type, but it is not associated with any data type.

Syntax of void pointer
void *ptr;

In C++, we cannot assign the address of a variable to the variable of a different data type. Consider the following example:

int *ptr; // integer pointer declaration
float a=10.42; // floating variable initialization
ptr= &a; // This statement throws an error.

In the above example, we declare a pointer of type integer, i.e., ptr and a float variable, i.e., ‘a’. After declaration, we try to store the address of ‘a’ variable in ‘ptr’, but this is not possible in C++ as the variable cannot hold the address of different data types.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
     int *ptr;  
    float f=10.42;  
    ptr = &f; // error  
    cout << "The value of *ptr is : " <<*ptr<<endl;  
    getch();
    return(0);
}

Output

Note:
An error will get displayed

C++ has overcome the above problem by using the C++ void pointer as a void pointer can hold the address of any data type.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
	void *ptr;   // void pointer declaration  
  	int a=20;   // integer variable initialization  
  	ptr=&a;   // storing the address of 'a' variable in a void pointer variable.  
  	cout<<a<<endl;
	cout<<&a <<endl;  
  	cout<<ptr<<endl;   
  	getch();
	return(0);
}

Output

20
0x6ffe14
0x6ffe14

Difference between void pointer in C and C++

In C, we can assign the void pointer to any other pointer type without any typecasting, whereas in C++, we need to typecast when we assign the void pointer type to any other pointer type.

Let’s understand through a simple example.

In C,

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

In the above program, we declare two pointers ‘ptr’ and ‘ptr1’ of type void and integer, respectively. We also declare the integer type variable, i.e., ‘a’. After declaration, we assign the address of ‘a’ variable to the pointer ‘ptr’. Then, we assign the void pointer to the integer pointer, i.e., ptr1 without any typecasting because in C, we do not need to typecast while assigning the void pointer to any other type of pointer.

Output:

value of a : 100
address of a : 6487564
address of a : 6487564
value of *ptr1 : 100

Example : In C++

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

In the above program, we declare two pointer variables of type void and int type respectively. We also create another integer type variable, i.e., ‘data’. After declaration, we store the address of variable ‘data’ in a void pointer variable, i.e., ptr. Now, we want to assign the void pointer to integer pointer, in order to do this, we need to apply the cast operator, i.e., (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 *).

Output:

value of a : 100
address of a : 0x6ffdfc
address of a : 0x6ffdfc
value of *ptr1 : 100