VOOZH about

URL: https://www.geeksforgeeks.org/c/void-pointer-c-cpp/

⇱ void Pointer in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

void Pointer in C

Last Updated : 14 Nov, 2025

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.

Example of Void Pointer in C


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.

Properties of Void Pointers

1. Void pointers cannot be dereferenced.

Example

The following program doesn't compile.


Output

Compiler Error: 'void*' is not a pointer-to-object type

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


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


Output
2

Time Complexity: O(1)
Auxiliary Space: O(1)

Note: The above program may not work in other compilers.

Advantages of Void Pointers in C

Following are the advantages of void pointers

  • 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 in C are used to implement generic functions in C. For example, compare function which is used in qsort().
  • void pointers used along with Function pointers of type void (*)(void) point to the functions that take any arguments and return any value.
  • void pointers are mainly used in the implementation of data structures such as linked lists, trees, and queues i.e. dynamic data structures.
  • void pointers are also commonly used for typecasting.
Comment
Article Tags: