Void Pointer (Generic Pointer)

  • In basic pointer, we have to declare integer, character and float pointer individually. But with the help of  ‘void pointer’ you have no need to declare individually which means, we can assign one pointer to all this three variable (int, char & float).
  • While dealing with the void pointer, one must & should declare typecasting (implicit) at the time of de-referencing the value.

For example :

1. main()
         char ch = 'a';
         int i = 10;
         float f = 22.5;
         void *p;
         p = &ch;
         printf("%c \n", *(char *)p);  //here void pointer will work as a character pointer
         p = &i;
         printf("%d \n", *(int *)p);   //here void pointer will work as a integer pointer
         p = &f;
         printf("%f \n", *(float *)p);  //here void pointer will work as a float pointer
Answer: a  


Null Pointer:

  • A pointer holding zero as an address is called as a Null pointer.

For example :

   int i = 10 , *p = 0; //here p = 0 means pointer has to points 0th address
   printf("%d \n", *p);  // so we get segmentation fault because this address 0th 
}                          // is not reserved for program

Use :

  • Once work with the pointer is finished, null the pointer otherwise it will affect the further program.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: