Pointers to void in C


In c programming language, pointer to void is the concept of defining a pointer variable that is independent of datatype. In C programming language, void pointer is a pointer variable used to store the address of variable of any datatype. That means single void pointer can be used to store address of integer variable, float variable, character variable, double variable or any structure variable. We use the keyword "void" to create void pointer. We use the following syntax for creating pointer to void…

void *pointerName ;

Example

void *ptr ;

Here, "ptr" is a void pointer variable which is used to store the address of any datatype variable.

Points to be Remembered

  1. void pointer stores the address of any datatype variable.

Example Program

#include<stdio.h>
#include<conio.h>

void main()
{
   int a ;
   float b ;
   char c ;
   
   void *ptr ;
   
   clrscr() ;

   ptr = &a ;
   printf(“Address of integer variable ‘a’ = %u\n”, ptr) ;
   
   ptr = &b ;
   printf(“Address of float variable ‘b' = %u\n”, ptr) ;
   	
   ptr = &c ;
   printf(“Address of character variable ‘c’ = %u\n”, ptr) ;
   
   getch() ;
}