Application of void pointer in c language

void pointer in c

In C language pointer is used to store the address of any variable or function but we need to remember that types of the pointer should be same the types of the pointed object (variable, pointer, array, function …etc.).

For example, if I want to store the address of the character, the pointer should be a pointer to the character.

To resolve the above problem, C language introduces a generic type of pointer that can store the address of any types.



What is the void pointer (Generic pointer)?

A void pointer is a generic pointer, it has no associated data type. It can store the address of any type of object and it can be type-casted to any types. According to C standard, the pointer to void shall have the same representation and alignment requirements as a pointer to a character type.A void pointer declaration is similar to the normal pointer, but the difference is that instead of data types we use the void keyword.

Syntax:

void * Pointer_Name;

void *pvHandle;

Dereferencing a void pointer

Using the indirection operator (*) we can get back the value which is pointed by the pointer, but in case of void pointer we cannot use the indirection operator directly. This is because a void pointer has no data type that creates a problem to the compiler to predict the size of the pointed object. So before dereferencing the void * we have to type cast it, it enables the compiler to predict the data types.

Let’s see some example code.

Explanation: When we compile the above code, we will get the compiler error because in the above code I tried to dereference the void pointer without casing.

But what happened if we type-cast the void pointer, its working fine see the below example code.




Why is void pointers use?

A very important feature of the void pointer is reusability. Using the void pointer we can store the address of any object and whenever required we can get back the object through the indirection operator with proper casting.

Let’s take an example,

void pointer

Explanation: In above code, pvData is a void pointer. Using it I am storing the address of the different variables (float, int, and char) and after that getting back their values using the indirection operator and proper typecasting.
You can see in the example code, how a single pointer is dealing with different types of variable. This is a very interesting feature of the void pointer that makes the programmer helpless to use the void pointer.



 Some important points for the void pointer

  • Like the other pointers, we cannot dereference the void pointers because the compiler does not have any information of the pointed object. If we try to compile the below code then we will get the compiler error.

But with proper typecasting, we can dereference the void pointer and get back the value of the pointed address.

  • According to c standard arithmetic operation on void pointers is illegal that means the C standard doesn’t allow pointer arithmetic with void pointers. However, In GNU C, addition and subtraction operations are supported on void pointers to assuming the size of the void is 1.

Output: 300 or compiler error.

Explanation: When we compile the code then some compiler throw the compiler error but some compiler compiled the code and print 300 as output to assume the size of the void 1.

Note: Don’t perform the arithmetic operation on the void pointer.

  • As per the C standard sizeof is not applicable on void but in GNU C we can calculate the size of the void and sizeof operator return 1.




Some important advantages of void pointer

  • Using the void pointer we can create a generic function that can take arguments of any data type. The memcpy and memmove library function are the best examples of the generic function, using these function we can copy the data from the source to destination.

Below code shows the implementation of memcpy in c

Using the memcpy we can copy the string, as well as the array of integers, see the below code.

You can see how is Memcpy work here like generic copy function with the help of void pointer.

  • We have already know that void pointer can be converted to another data type that is the reason malloc, calloc or realloc library function return void *. Due to the void * these functions are used to allocate memory to any data type.
  • Using the void * we can create a generic linked list.For more information see this link: How to create generic Link List.

Your opinion matters

In this article, I have tried to cover a lot of points for the void pointer but I would love to know your opinion on the void pointers. So please don’t forget to write a comment in the comment box.




7 comments

  1. for a=1;
    b=a++++a;
    b=4,a=3;
    for a=1
    b=++a+a++
    b=5,a=3;
    How they come
    Which have high priority between a++&++a?
    Which operand is evaluated first in each expression?
    Please Explain in detail?

Leave a Reply