Void Pointers -Part 3
Hello all, This is part 3 of the Pointers in C. If you came here directly go back and learn about the brief introduction about pointers. After reading that it will be more efficient. Lets get into it
- What is Void Pointer ?
- Uses of Void pointer in C
- Void Pointers example
- Pointers with Unsigned Decimal
What is Void Pointer ?
A Void Pointer is a pointer which doesn’t depend on datatype. It allocates any address and typecasts into it.
Lets go with a simple example, see the below snippet of code:
a = 10;
b = 'x';
*p = &a; // void pointer holds address of int 'a'
p = &b; // void pointer holds address of char 'b'
In the example the void pointer typecasts the values without considering dataype.
Advantages of Void Pointer
- malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of any data type (just because of void *)
- void pointers cannot be dereferenced. Eg: The following code can’t be compiled
It display the output as: