添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

// C Program to demonstrate that a void pointer
// can hold the address of any type-castable type
#include <stdio.h>
int main()
int a = 10;
char b = 'x' ;
// void pointer holds address of int 'a'
void * p = &a;
// void pointer holds address of char 'b'
p = &b;

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

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.

void * ptr = &a;
// The void pointer 'ptr' is cast to an integer pointer
// using '(int*)ptr' Then, the value is dereferenced
// with `*(int*)ptr` to get the value at that memory
// location
printf ( "%d" , *( int *)ptr);
return 0;

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.

// C program to demonstrate the usage
// of a void pointer to perform pointer
// arithmetic and access a specific memory location
#include <stdio.h>
int main()
// Declare and initialize an integer array 'a' with two
// elements
int a[2] = { 1, 2 };
// Declare a void pointer and assign the address of
// array 'a' to it
void * ptr = &a;
// Increment the pointer by the size of an integer
ptr = ptr + sizeof ( int );
// The void pointer 'ptr' is cast to an integer
// pointer using '(int*)ptr' Then, the value is
// dereferenced with `*(int*)ptr` to get the value at
// that memory location
printf ( "%d" , *( int *)ptr);
return 0;

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.
  • Difference between Dangling pointer and Void pointer
    Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer: By deallocating memoryFunction CallWhen the variable goes out of scope Void pointer: Void pointer is a specific pointer type – void * – a pointer that point
    C - Pointer to Pointer (Double Pointer)
    Prerequisite: Pointers in C The pointer to a pointer in C is used when we want to store the address of another pointer. The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers. We can use a pointer to a pointer to cha
    What is a Pointer to a Null pointer
    NULL pointer in C At the very high level, we can think of NULL as a null pointer which is used in C for various purposes. Some of the most common use cases for NULL are To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet. C/C++ Code int* pInt = NULL; To check for a null pointer before accessing an
    Pointer to an Array | Array Pointer
    Prerequisite: Pointers Introduction  Consider the following program:  C/C++ Code #include&lt;stdio.h&gt; int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; printf(&quot;%p\n&quot;, ptr); return 0; } In the above program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer that can po
    How does 'void*' differ in C and C++?
    C allows a void* pointer to be assigned to any pointer type without a cast, whereas in C++, it does not. We have to explicitly typecast the void* pointer in C++ For example, the following is valid in C but not C++: void* ptr; int *i = ptr; // Implicit conversion from void* to int* Similarly, int *j = malloc(sizeof(int) * 5); // Implicit conversion
    Is it fine to write void main() or main() in C/C++?
    In C/C++ the default return type of the main function is int, i.e. main() will return an integer value by default. The return value of the main tells the status of the execution of the program. The main() function returns 0 after the successful execution of the program otherwise it returns a non-zero value. Using void main in C/C++ is considered no
    Difference between "int main()" and "int main(void)" in C/C++?
    Note: This was true for older versions of C but has been changed in C11 (and newer versions). In newer versions, foo() is same as foo(void). Consider the following two definitions of main(). Definition 1: C/C++ Code int main() { /* */ return 0; } C/C++ Code int main() { /* */ return 0; } Definition 2: C/C++ Code int main(void) { /* */ return 0; } C
    Return From Void Functions in C++
    Void functions are known as Non-Value Returning functions. They are "void" due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Void functions do not have a return type, but they can do return values. Some of the cases are list
    Dangling, Void , Null and Wild Pointers in C
    In C programming pointers are used to manipulate memory addresses, to store the address of some variable or memory location. But certain situations and characteristics related to pointers become challenging in terms of memory safety and program behavior these include Dangling (when pointing to deallocated memory), Void (pointing to some data locati
    How to declare a pointer to a function?
    While a pointer to a variable or an object is used to access them indirectly, a pointer to a function is used to invoke a function indirectly. Well, we assume that you know what it means by a pointer in C. So how do we create a pointer to an integer in C? Huh..it is pretty simple... int *ptrInteger; /*We have put a * operator between int and ptrInt
    We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab.
    You will be notified via email once the article is available for improvement. Thank you for your valuable feedback!
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    Suggest Changes
    Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.