void *
is a generic pointer, but what about void **
? Is void **
also a generic pointer?
Can we typecast void **
t
Void ** a generic pointer?
void **
is not a generic pointer. Standard says only about void *
to be a generic pointer.
One side point about pointers to pointers and memory allocation: although the
void *
type, as returned bymalloc
, is a generic pointer, suitable for assigning to or from pointers of any type, the hypothetical typevoid **
is not a generic pointer to pointer.
Can we typecast
void **
toint **
,char **
and so on.
No. You should not.
C-FAQ says that:
There is no generic pointer-to-pointer type in C.
void *
acts as a generic pointer only because conversions (if necessary) are applied automatically when other pointer types are assigned to and fromvoid *
's; these conversions cannot be performed if an attempt is made to indirect upon avoid **
value which points at a pointer type other thanvoid *
. When you make use of avoid **
pointer value (for instance, when you use the*
operator to access thevoid *
value to which thevoid **
points), the compiler has no way of knowing whether thatvoid *
value was once converted from some other pointer type. It must assume that it is nothing more than avoid *
; it cannot perform any implicit conversions.In other words, any
void **
value you play with must be the address of an actualvoid *
value somewhere; casts like(void **)&dp
, though they may shut the compiler up, are nonportable (and may not even do what you want; see also question 13.9). If the pointer that thevoid **
points to is not avoid *
, and if it has a different size or representation than avoid *
, then the compiler isn't going to be able to access it correctly.
No. void**
is a pointer to void*
, and nothing else. Only void*
acts like a generic pointer.
Note that actually trying it will probably yield consistent results, but only the above is mandated by the Standard, anything else is Undefined Behaviour and may crash without mercy.
void**
is a pointer to void*
. On top of it being undefined behavior (which while scary, the behavior is often in effect defined by your compiler), it is also a huge problem if you do the reinterpret_cast
:
int x = 3;
char c = 2;
int* y = &x;
void* c_v = &c; // perfectly safe and well defined
void** z = reinterpret_cast<void**>(&y); // danger danger will robinson
*z = c_v; // note, no cast on this line
*y = 2; // undefined behavior, probably trashes the stack
Pointers to pointers are very different beasts than pointers. Type changes that are relatively safe on pointers are not safe on pointers to pointers.