why is sizeof void pointer 2 ?
The size of a void*
is a platform dependent value. Typically it's value is 4 or 8 for 32 an 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 / operation system?
What makes you think it is ? Try compiling and running the following code and i am pretty sure the size won't be 2.
int main(int argc, char** argv)
{
void *foo = 0;
printf("%u",sizeof(foo));
}
As JaredPar already pointed out, this is platform dependant. To put it differently: How many bits does the used CPU use for memory-addressing? For 16bit adresses you would get a size of 2 bytes. Are you compiling code for a 16bit microcontroller?
A pointer stores a memory address that points to something else. The size of a pointer depends on your platform. On a 32 bit platform you need 32 bits or four bytes to store a memory address so sizeof any pointer will return 4.
If sizeof(void*) is 2 you're probably running on a 16 bit platform.
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.