Mar 11, 2015

C Programming #51: Void pointer

Please have glance at C Programming#41 Pointer - introduction before reading this article further.

We already know that pointer is variable that store address. Pointer is also associated with type like character pointer, integer pointer, float pointer. Say we have a reason to just store a address not associate it to any type, in such situation void pointer will come in handy.



Syntax: 


void *pv;

pv is void pointer, which can store address in it and not associated with any data type.

Below is a small program to demonstrate the usage of void pointer



#include <stdio.h>

int main()
{
   void *pv;
   int a = 10;
   char c = 'a';
   float f = 1.1;

   /* store the address of variable a in pv */
   pv = &a;
   printf("Address of a is %p; value of pv is %p\n", &a, pv);

   /* store the address of variable c in pv */
   pv = &c;
   printf("Address of c is %p; value of pv is %p\n", &c, pv);

   /* store the address of variable f in pv */
   pv = &f;
   printf("Address of f is %p; value of pv is %p\n", &f, pv);

   return 0;                                                                  
}

Output of the above program 

Address of a is 0x7fffa72e09d4; value of pv is 0x7fffa72e09d4
Address of c is 0x7fffa72e09df; value of pv is 0x7fffa72e09df
Address of f is 0x7fffa72e09d8; value of pv is 0x7fffa72e09d8

pv was used to store the address of variable a, c and f. pv is like a generic pointer that can store address of any type (variable and function).

Now say i have written below program
 


#include <stdio.h>

int main()
{
   void *pv;
   int a = 10;

   /* store the address of variable a in pv */
   pv = &a;
   printf("Address of a is %p; value of pv is %p\n", &a, pv);
   printf("pv is poiting to %d\n", *pv);
   return 0;                                                                  
}

If i try to compile above code i get following error

a.c: In function 'main':
a.c:11:34: warning: dereferencing void * pointer [enabled by default]
a.c:11:2: error: invalid use of void expression

Basically compiler is giving error telling that void pointer cannot be de-referenced.

Why cannot the void pointer referenced ?

To understand this we need to understand why is a datatype associated with pointer.
To explain it let me write a very simple C program and explain via it.


#include <stdio.h>

int main()
{
   int a = 10;
   int *pa = &a;

   printf("Value of pa is %p, de-referencing pa %d\n", pa, *pa);              

   return 0;
}

Output of the able program is

Value of pa is 0x7fff086e385c, de-referencing pa 10

In the above program
  • Address of a is 0x7fff086e385c which stores the value 10.
  • pa is pointer to integer which stores the address of variable (hence conventionally we call pa points to a).

Now when we de-reference a pointer using *pa, compiler gets the address 0x7fff086e385c stored in pa. Integer is stored in that address, but it needs to know how may bytes to access to get data stored. In this case since the type of the pointer is a integer it would access 4 bytes from that address to retrieve the value 10 which would be stored in some binary format. Hence for de-reference to work not only the address but the type associated with pointer is important to know how may bytes of memory to be accessed


Now we know how the de-referencing working internally. Going back to the original issue where *pv was not allowed. When we do *pv, complier knows from the value of pv which address to look up to. But since void pointer is not associated with any datatype compiler is left clueless to how may bytes to access. That is why de-referencing of the void pointer is not allowed.


Hence generally void pointers are used to store plain address in them not associated with any type.



Links

Next Article - C Programming #52: Passing array to a function
Previous Article - C Programming #50: Double pointer - inception
All Article - C Programming

No comments :

Post a Comment