Q

Emertxe-News & Blog

Home » Blog » Void Pointers : Its Genericness
Void Pointers : Its Genericness

Void Pointers : Definition

Void pointers in C programming are the pointer variables which has no specific data type. These are also called as Generic pointers because it can point to any data type. In General, the compiler has no idea what type of object a void Pointer really points to.

Void Pointers : Declaration

A void pointer is declared like a normal pointer, using the keyword “void” as the pointer’s type as shown below,

 
Void pointers in C programming
 

Referencing & De-referencing the void pointers

In order to explain, how void pointers can be referenced & De-reference, let we take a small sample code as shown below in Fig 1,

 

Void pointers in C programming
 Fig 1: Referencing & De-referencing of the void pointers
 

From the above Fig 1, it is clear that while referencing the void * no need to typecast, only when De-referencing the void *, it need to be type-casted otherwise the gcc compiler will give warning along with error note as shown below in Fig 2,

Void pointers in C programming
Fig 2: Result obtained while de-referencing the void pointer without typecasting.
Void pointers in C programming
  Fig 3: Output after typecasting the void pointer while dereferencing.

Why void pointers are useful ?

One main advantage of using the void pointers is that it can be re-usable. Because of this feature of the void pointers ‘space’ & ‘time’ complexities can be reduced to some extent which is very critical in Embedded System designing.

They are very useful when you want a samepointer to point to data of different types at different times.

Let us consider a small simple code snippet, to show how void pointers can be re-usable which reduces the space complexity in theexample as shown in the Fig 4 given below,

 
 Void pointers in C programming
Fig 4: Void pointer reusability.
Void pointers in C programming
Fig 5: Output of the code shown in Fig 4.

For more clarity let us understand the above code through pictorial representation.

Line No.14 : The vptr is pointing to the address of the integer variable ‘i’, as shown below in Fig 6.1

Line No.18: The vptr is pointing to the address of the charactervariable ‘c‘, as shown below in Fig 6.2

LineNo.22: The vptr is pointing to the address of the floatvariable ‘f‘, as shown below in Fig 6.3

Line No.26: The vptr is pointing to the address of the doublevariable ‘d‘, as shown below in Fig 6.4

Void pointers in C programming
 
 

Note : xxxx, yyyy, zzzz, aaaa, bbbb are all some arbitrary addresses shown in the above figures from 6.1 to 6.4.

From the figures 6.1 to 6.4 , it is very clear that we have used only one pointer i.e vptr , instead of four different pointers to point to four different variable of different data types, where space complexity is more. Only one thing that the programmer should keep in the mind while coding is that proper typecasting should be done depending on the different data types.

Void pointers are used in places where you need to work with different pointer types in the same code. One commonly usedexample is the library function qsort:

 

baseis the address of an array, nmembis the number of elements in the array, sizeis the size of each element, and comparis a pointer to a function that compares two elements of the array.

Pointer Arithmetic on Void*

Let us learn how to apply pointer arithmetic on the void pointers through the bug code as shown in the Fig 7 given below.

 
Fig 7: Bug code
Fig 8: Output of the bug code shown in Fig 7
 

From the above result, one can see that we are not getting the expected output. Surprisingly what went wrong ? Let us analyze the above code & output we got.

1. From the output it is very clear that addresses are incremented by one one byte, this is because the gcc compiler assumes sizeof(void) is 1 byte long.

2. So, when we increment void pointer by one, it is incremented by one byte long as one can observe this from the Fig 8(address part).

3. Now coming to the value part, the first value i.e arr[0] is getting printed correct, but after that all the values are wrong, this I am explaining through the pictorial representation as shown in the Fig 9

to Fig 13 below.

Note : The output depends on the endianness of the architecture.
Fig 9
Fig 10
Fig 11
Fig 12
Fig 13
 

From the above five figures, we can see why we are not getting the expected output. So the corrected code along with expected output is shown in the Fig 14 & 15 given below,

Fig 14 : Correction to the code given in Fig 7
Fig 15: Expected output for the code given in Fig 11.
 

Void pointers in C programming can be assigned to any pointer value. One cannot dereference a void pointer without proper typecasting. Functions such as malloc, calloc, free utilize void pointers.

YOU MAY ALSO LIKE

Emertxe’s Online Training Programs in Embedded Systems & IoT

Emertxe’s Online Training Programs in Embedded Systems & IoT

The reason why Emertxe is the best choice for career advancement: eLearning and Online training programs are the way forward in the COVID-19 disrupted world. Riding along the digital revolution will ensure engineers are future-ready with skills to not only secure but...

Our Training Programs for Freshers

Our Training Programs for Freshers

Introduction: Emertxe is the leading training institute in Bangalore for Embedded Systems and IoT domains. It is a pioneer in training freshers since 2003 by providing excellent placement opportunities for freshers. Over the years 70000+ students have made their...