Void Pointers

  • Before taking a deep dive into what are Void Pointers ,WE Suggest you to
    please go through to this Topics : What are Pointers?
  • What are Void Pointers ?
  • As you know Pointers are the variable which stores the address of an another variable,which is nothing but location of that variable
  • As You know , Pointers can only store the address of variable only when their data types are same.
  • For example:int type of pointer can only store address of integer type of variable.
  • Float type of pointer can only store address of float type of variable And so on.
  • By over coming from the above functionality, Void Pointer Comes in Action.

Void Pointers

  • Void Pointers are the pointer Which can store the address of any kind of data type.
  • Just while assigning and Accessing the Pointer or variable which is point by the pointer you have to do type casting.
  • Let's learn how to declare , initialize , and type casting of an Void pointers.

Syntax: Declaration of void Pointer
    void  * Pointer_name;

Let's declare an pointer variables:
#include<stdio.h>
void main()
{
    
    
    void *p1; //declaration of void pointer
    
    
    void *p2; //declaration of void pointer
    
    
    void *p3; //declaration of void pointer
}

Method of Initializing the void pointer will remain same:
#include<stdio.h>
void main()
{

    int a = 10;
    void *p1; //declaration of void pointer
    
    //initializing the void pointer
    p1 = &a;  //assigning the address of a variable to 
              //void pointer p1.

}

Syntax of Accessing the data which is Pointed by the Pointer:
*(datatype_of_stored_variable *)pointer_name;

Let's Access the data which is point by the pointer:
#include<stdio.h>
void main()
{

    int a = 10;
    void *p1; //declaration of void pointer

    //initializing the void pointer
    p1 = &a;  //assigning the address of a variable to
              //void pointer p1.

    value = *(int *)p1 // storing the value which is point 
                      //by the pointer p1 to variable value
                      
                      
    printf("\nData which is  point by the pointer p1 is %d",value);
    
    
}

Output
Data which is point by the pointer p1 is 10.





Now we will store and access the address and value of float type of variable.:
#include<stdio.h>
void main()
{

    float a = 7.1;
    void *p2; //declaration of void pointer

    //initializing the void pointer
    p2 = &a;  //assigning the address of a variable to
              //void pointer p2.

    value = *(float *)p2 // storing the value which is point 
                      //by the pointer p2 to variable value
                      
                      
    printf("\nData which is  point by the pointer p2 is %f",value);
    
    
}

Output
Data which is point by the pointer p2 is 7.1.





Now we will store and access the address and value of char type of variable.:
#include<stdio.h>
void main()
{

    char c = 'A';
    void *p3; //declaration of void pointer

    //initializing the void pointer
    p3 = &a  //assigning the address of c variable to
              //void pointer p3.

    value = *(char *)p3 // storing the value which is point 
                      //by the pointer p3 to variable 'value'
                      
                      
    printf("\nData which is  point by the pointer p3 is %c",value);
    
    
}

Output
Data which is point by the pointer p3 is A.





Simple program: Using above concept
#include<stdio.h>
void main()
{
   int result1;
   float result2;

    int a = 10,b=30;

    float c = 7.5,d = 9.5;


    void *p1,*p2,*p3,*p4; //declaring the void pointers. 
    
    p1 = &a   //assigning the address to
    p2 = &b;   //pointer variables
    
    p3 = &c;  
    p4 = &d;  


    result1 = *(int *)p1   +  *(int *)p2; //catch1



     result2 = *(float *)p3   +  *(float *)p4; //catch2




    printf("\nSum of two variable a + b = %d",result1);

    printf("\nSum of two variable c + d = %f",result2);
}





  • Catch1: storing the addition of two integer values which is pointed by the pointer p1 and p2 to variable 'result1'
  • Catch2: storing the addition of two float values which is pointed by the pointer p3 and p4 to variable 'result2'


  • Output
    Sum of two variable a + b = 40

    Sum of two variable c + d = 17.00000.





    • This is how void pointer works.
    • Void pointer becomes pretty much complicated at the time of accessing the data.
    • There is no harm for using the normal pointers ,Just it is a Good thing that you know what are void Pointers, as in many interviews this question is asked that what are Void pointers.
    • So , it is good thing to know what are void pointers.
    • So, do more practice for having a good command on void pointers.




    Further Concept:




    Further Topics:



    People Also Searched: