What is void pointer? | SoloLearn: Learn to code for FREE!


What is void pointer?

void *ptr;

c++ c

2 Answers

New Answer


Nagaraj P void pointer (void*) also called opaque pointer is a placeholder to store pointer to anytype. It is used to get some level of genericity in the code. On it's own it is not usable, hence it must be converted to pointer to appropiate type before doing any pointer arithmetic or dereferencing. Pointer to any type is usually implicitly converted to pointer to void, but sometimes explicit conversion is required. int x = 10; int *pint = &x;// pointer to int void *pvoid = pint; // implicit conversion void *pvoid1 = (void*)pint; // explicit conversion C style, works in C++ too void* pvoid = reinterpret_cast<void*>(pint); C++ style *pvoid = 11; // error dereferencing void pointer pvoid ++; // illegal pointer arithmetic. correct way to dereference *((int*)pvoid) = 11; explicitly convert to correct pointer first ((int*)pvoid)++; // pointer arithmetic.


In simple word the used of this statement is when we dont know beforehand what will the type of data will be that this pointer is pointing on.