![]() |
VOOZH | about |
A void pointer is a pointer that has no associated data type with it. A void pointer can hold an address of any type and can be typecasted to any type.
Time Complexity: O(1)
Auxiliary Space: O(1)
If you're looking to master pointers, including how they work with data structures, the C Programming Course Online with Data Structures covers pointers extensively with practical use cases.
1. Void pointers cannot be dereferenced.
Example
The following program doesn't compile.
Output
Compiler Error: 'void*' is not a pointer-to-object typeThe below program demonstrates the usage of a void pointer to store the address of an integer variable and the void pointer is typecasted to an integer pointer and then dereferenced to access the value. The following program compiles and runs fine.
10
Time Complexity: O(1)
Auxiliary Space: O(1)
2. The C standard doesn't allow pointer arithmetic with void pointers. However, in GNU C it is allowed by considering the size of the void as 1.
Example
The below C program demonstrates the usage of a void pointer to perform pointer arithmetic and access a specific memory location. The following program compiles and runs fine in gcc.
2
Time Complexity: O(1)
Auxiliary Space: O(1)
Note: The above program may not work in other compilers.
Following are the advantages of void pointers