How to use Void pointer in C++?

In this post, we are going to learn about void pointers and their use in C++. This post will help you to better understand the concept of a void pointer using C++. After that, we will implement void pointers in code.

What is Pointer?

The pointer is variable which stores the address of another variable. these pointers are represented with “*” before variables.

int *pointer; //this is pointer variable, we declare it as shown here int is a data type.
int variable; //this is a normal int type variable.

*pointer= &variable;//the address of the variable is stored in ptr.

In the above code first, we declared a variable of type pointer i.e pointerthis type of variable only stores the address of another variable.
In the second line of code, we declared a normal integer type variable. Then the assigned address of a variable to pointer variable i.e pointer.

What is a Void pointer?

As shown in the above program, we know how to use pointers. Now void pointers are a pointer that does not specify the data type at the type of declaration, and it can point to any type of variable, just need to specify the data type that is to be assigned. we used int *pointer to declare a normal pointer but we declare void pointer as void *void_pointer. Void pointers are also called as Generic Pointers. Let us understand this through an example.

CODE:

#include<iostream>
using namespace std;
int main()
{
int number=10;
char variable='A';

//this is integer type of pointer
int *int_pointer;

//this is void type of pointer.
void *void_pointer;

//Assigning number variable to integer type pointer.
int_pointer=&number;

//Assigning number variable to void type pointer.
void_pointer=&number;

//printing value of number variable using int_pointer.
cout<<*int_pointer<<endl;

//printing Value of number variable using void_pointer.
cout<<*(int*)void_pointer<<endl;

// Cannot assign cha type variable to int type pointer.
//int_pointer=&variable;

//Assigning char type variable to void type pointer.
void_pointer=&variable;

//printing value of variable.
cout<<*(float*)void_pointer;
return 0;
}

 

As shown in the above program code, Int type pointer’s can’t be assigned to any other type of variable. As void pointer can be assigned with every data type i.e int, char, float, double, etc. Thus these void pointers are dynamic to use in a program where we use a single pointer to iterate over various data type variables.

OUTPUT:

10

10

0.5

How to use void pointers?

C language does not allow us to perform an arithmetic operation over void pointers. Declare void pointer i.e void *ptr as mentioned below and assign the address of int variable we have declared. then we have stored the value from that void pointer to the variable val and we have printed that value. while retrieving the value from void pointers you have to specify the variable type i.e *(variable_type *)ptr.

 

#include<stdio.h> 
int main() 
{ 
  int a=10;
  void *ptr = &a; 
  
  int val=*(int*)ptr;
    
  cout<<val<<endl;
  return 0; 
} 

 

OUTPUT:

10

As you can see in the above output you can assign a void pointer to both types of variables i.e int, float.

One response to “How to use Void pointer in C++?”

  1. Preeti Shrivastav says:

    Very helpful and informative content with clear language which is easy to understand

Leave a Reply

Your email address will not be published. Required fields are marked *