void pointer in C, you should know

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 (void pointer) that can store the address of any types.

What is the void pointer in c?

A void pointer in c is called 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;

What is the size of a void pointer?

The size of a void pointer is similar to the size of the character pointer. According to C standard, the pointer to void shall have the same representation and alignment requirements as a pointer to a character type.

The size of the pointers depending on the platform and it can be 2bytes, 4bytes or 8bytes …etc.

Let’s see some example code.

 

Output: On a 64bit machine

void pointer in c

Dereferencing a void pointer in C

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 for 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 type 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.

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

C tutorial

Application of void pointer in C 

Application of void pointers are very wide, we can not cover all the application in one article. Here I am taking one of the most popular applications of the void pointer in qsort function.

A qsort is a C standard library function that use to sort arrays. Using the qsort function, we can sort the array of integer, double, long etc.

Following is the declaration for qsort() function,

void qsort(void *arr, size_t elements, size_t size, int (*comp)(const void *, const void*));

Parameters of qsort:

arr − pointer to the first element of the array.

elements  − number of elements in the array.

size − size(in bytes) of the element in the array.

comp − compare function that is used to compares two elements.
int comp(const void* a, const void* b);

Let see an example code to understand the working of qsort and importance of void pointer:

In this example code, I am showing how is the qsort function sort any type of array with the help of compare function.

Output:

Void pointer in c

Disadvantages of the void pointer in C

  • Like the other pointers, we cannot dereference the void pointers because the compiler does not have any information about 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.

Advantages of the void pointer in c

  • 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.

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