void pointer in C / C++

14 Jun, 2022
A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typecasted to any type. 
C++
C
// C++ Program to demonstrate that a void pointer// can hold the address of any type-castable type #include <iostream>using namespace std; int main(){    int a = 10;    char b = 'x';         void* p = &a;  // void pointer holds address of int 'a'    p = &b; // void pointer holds address of char 'b'} // This code is contributed by sarajadhav12052009
int a = 10;char b = 'x'; void *p = &a;  // void pointer holds address of int 'a'p = &b; // void pointer holds address of char 'b'
Advantages of void pointers: 1) 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 *) 
Note that the above program compiles in C, but doesn’t compile in C++. In C++, we must explicitly typecast return value of malloc to (int *). 2) void pointers in C are used to implement generic functions in C. For example compare function which is used in qsort(). Some Interesting Facts: 1) void pointers cannot be dereferenced. For example the following program doesn’t compile. 
C++
C
// C++ Program to demonstrate that a void pointer// cannot be dereferenced #include <iostream>;using namespace std; int main(){    int a = 10;    void* ptr = &a;       cout << *ptr;       return 0;} // This code is contributed by sarajadhav12052009
#include <stdio.h>int main(){    int a = 10;    void *ptr = &a;    printf("%d", *ptr);       return 0;}
Output:
Compiler Error: 'void*' is not a pointer-to-object type 
The following program compiles and runs fine. 
C++
C
#include <iostream>using namespace std; int main(){    int a = 10;    void* ptr = &a;       cout << *(int *)ptr << endl;       return 0;} // This code is contributed by sarajadhav12052009
#include <stdio.h> int main(){    int a = 10;    void *ptr = &a;    printf("%d", *(int *)ptr);    return 0;}
Output:
10
2) The C standard doesn’t allow pointer arithmetic with void pointers. However, in GNU C it is allowed by considering the size of void is 1. For example the following program compiles and runs fine in gcc. 
C
#include<stdio.h>int main(){    int a[2] = {1, 2};    void *ptr = &a;    ptr = ptr + sizeof(int);    printf("%d", *(int *)ptr);    return 0;}
Output:
2
Note that the above program may not work in other compilers. References: http://stackoverflow.com/questions/20967868/should-the-compiler-warn-on-pointer-arithmetic-with-a-void-pointer http://stackoverflow.com/questions/692564/concept-of-void-pointer-in-c-programming Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
binuray
sarajadhav12052009
C Basics
C-Pointers
cpp-pointer
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Multidimensional Arrays in C / C++
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
std::string class in C++
Unordered Sets in C++ Standard Template Library
rand() and srand() in C/C++
Enumeration (or enum) in C
C Language Introduction
Memory Layout of C Programs