Let us learn about Void pointer in C programming and understand its implementation with an example, explanation, advantages, disadvantages and much more.
What is a Void Pointer in C Programming?
A void pointer is a pointer variable that has void as its data type and is not associated with any reference type. It is a general purpose pointer.
In simpler words, a void pointer does not have any data type associated with it and it can be used to hold the address of any data type.
A void pointer is also popularly known as a generic pointer and it is not the address of a character, an integer, a real or any other data type.
The generic pointer is a special type of pointer that can be used to point to variables of any datatype. It is declared using the keyword void as the pointer’s datatype.
How To Declare A Void Pointer?
void * ptr;
There is a restriction on creating variables of a void data type in C programming language. The void pointer cannot point to any data type, and, hence cannot be dereferenced.
It is, therefore, important to typecast a void pointer to another type of pointer before using it. A compiler error will be generated if you assign a pointer of one data type to a pointer of another data type without a cast.
A generic pointer is often used when you want a pointer to point to data of different data types at different times. It is, therefore, important to type case a void pointer to another type of pointer before using it.
One important point to note is that a pointer variable of type void * cannot be dereferenced as the void pointer does not know what type of object it is pointing to.
Therefore, it cannot be dereferenced directly. Rather, the void pointer must first be explicitly cast to another pointer type before it is dereferenced.
It is, therefore, important that the void pointer must first be explicitly cast to another pointer type before it is dereferenced.
Note: It is always recommended to avoid using a void pointer in C programming unless absolutely necessary, as they allow you to avoid type checking.
The major difference between Void pointer and a Null pointer is that the former cannot be dereferenced whereas a Null pointer is assigned to a constant NULL.
Note: This example of a void pointer in C programming is compiled with GNU GCC compiler using CodeLite IDE on Microsoft Windows 10 operating system.
C Program To Implement Void Pointer
int num = 35;
char ch = 'M';
ptr = #
printf("\nThe Void pointer points to integer value:\t%d\n", *(int *)ptr);
ptr = &ch;
printf("\nThe Void pointer points to character value:\t%c\n", *(char *)ptr);
As you can see in the above code, the void * ptr is first assigned to an integer data type and then it is assigned to a character data type.
How To De-Reference Void Pointers?
Here’s a sample code which is self-explanatory and helps you to de-reference a void pointer.
char ch = 'X';
a = &ch;
printf("\n%c\n", *(char *)a);
Advantages of Void Pointers
- The GCC compiler allows using arithmetic operations on void pointers.
- The memory allocation functions such as malloc() and calloc() return void * type and hence, these functions can be used to allocate memory of any data type.
- A generic pointer can be used to hold the address of a variable of any data type.
Disadvantages of Void Pointers
- The C Standard does not allow arithmetic operations to be performed on void pointers.
If you have any doubts about void pointer in C programming, let us know about it in the comment section. Find more about generic pointers on StackOverflow.
|MORE ON POINTERS|
Thank you for the explanation. I was confused between generic pointers and void pointers. I thought they are different.
void *malloc(size_t size);
The malloc function also returns void* and then we usually typecast it to our preferred datatypes.