void pointer in C / C++
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++ 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 |
C
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 *)



