The size of a void* is a platform dependent value. Typically it's value is 4 or 8 bytes for 32 and 64 bit platforms respectively. If you are getting 2 as the value then your likely running on a 16 bit coding platform (or potentially have a coding error).
Could you post the code you are using and some more information about your environment / operating system?
Answer from JaredPar on Stack OverflowThe size of a void* is a platform dependent value. Typically it's value is 4 or 8 bytes for 32 and 64 bit platforms respectively. If you are getting 2 as the value then your likely running on a 16 bit coding platform (or potentially have a coding error).
Could you post the code you are using and some more information about your environment / operating system?
Per the online C standard (n1256 draft):
6.2.5 Types
...
27 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.39) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
As to why void and char pointers have a size of 2 on your system, I suspect that's because you're on a 16-bit platform.
Videos
Can you delete a void pointer?
What cannot be done on the void pointer?
The type void has no size; that would be a compilation error. For the same reason you can't do something like:
void n;
EDIT.
To my surprise, doing sizeof(void) actually does compile in GNU C:
$ echo 'int main() { printf("%d", sizeof(void)); }' | gcc -xc -w - && ./a.out
1
However, in C++ it does not:
$ echo 'int main() { printf("%d", sizeof(void)); }' | gcc -xc++ -w - && ./a.out
<stdin>: In function 'int main()':
<stdin>:1: error: invalid application of 'sizeof' to a void type
<stdin>:1: error: 'printf' was not declared in this scope
If you are using GCC and you are not using compilation flags that remove compiler specific extensions, then sizeof(void) is 1. GCC has a nonstandard extension that does that.
In general, void is a incomplete type, and you cannot use sizeof for incomplete types.
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:
The value stored in the pointer is an address to memory. If you're on a 32-bit system, that pointer into memory is going to be 32 bits (or four bytes) long. If you're on a 64-bit system, that pointer into memory is going to be 64 bits (or eight bytes) long.
The size of the data that holds the location in memory has nothing to do with the size of the data represented at that location in memory.
As for how a char * differs from a double *, the char * can point to any location, but the double * has to point to something along an eight-byte boundary. Larger data has to be aligned according to the rules of the processor you're on. So, pointers to small data are not generally compatible with pointers to large data (e.g. you shouldn't point a double * pointer to a char * address); but you're save going in the other direction (e.g. you can point a char * pointer to a double * address).
The value of pointer merely conveys the starting address of an object, not any information about its size.
The type of a pointer inherently conveys information about the size of the object it points to, if it is a complete type. However, void is incomplete, and a void * provides no information about the size of the object it originated from (except possibly by extensions to standard C and by debugging features).
Generally, if your program needs to know the size of what a void * points to, it must track that information itself.
*voidPtris invalid in standard C. It is a GCC extension and that is the reason whysizeof(*voidptr)is1. Any other (non GCC family) compiler will not compile this code.%dis the wrong format to printsize_t. You should use%zuinstead.There is no way in C to determine what size object is referenced by the
voidpointer