c: size of void*

facha picture facha · Aug 2, 2011 · Viewed 36.7k times · Source

I'm a bit confused with a void* pointer in C. Especially after reading this question: Is the sizeof(some pointer) always equal to four?, where one person says there is no guarantee that sizeof(int *) == sizeof(double *)

My question is: is there a guarantee of sizeof(void*) >= sizeof(any other pointer type)? In other words, can I always assign a some_type* pointer to a void* pointer and then get it back as some_type*?

Answer

cnicutar picture cnicutar · Aug 2, 2011

Only data pointers. void * can hold any data pointer, but not function pointers.

Here is a C FAQ.

void *'s are only guaranteed to hold object (i.e. data) pointers; it is not portable to convert a function pointer to type void *. (On some machines, function addresses can be very large, bigger than any data pointers.)

As for the first part, yes, different types can have pointers of different sizes: