why is sizeof
void pointer 2
?
-
Because that's the size of the pointer in your system. The size is expressed with respect to the size of a `char`. `char` is usually 8 bit long, but doesn't have to be. – Maciej Hehl Oct 04 '10 at 15:39
-
1It isn't. Or more specifically: It doesn't have to be. – sellibitze Oct 04 '10 at 17:58
-
1ya..u r right. its platform dependent – alfesani Oct 08 '10 at 06:27
-
2(off topic) I suggest you switch to Windows 10 because DOS is probably not the best coding platform :-) – Kotauskas Mar 11 '18 at 17:51
5 Answers
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?
-
4
-
@Paul, thanks updated for 16 bit OS's. I thought it much more likely that it was an error instead of people still using 16 bit OS's but that may be a poor assumption. – JaredPar Oct 04 '10 at 08:04
-
9for some reason a lot of colleges (especially those in India) still seem to use things like Turbo C and 16 bit DOS (as well as some very bad text books), which is why we tend to see a lot of questions on SO and elsewhere with e.g. `void main()` and assumptions about sizeof(void *) being 2, etc. – Paul R Oct 04 '10 at 08:49
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.

- 119,563
- 19
- 122
- 198
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.

- 36,795
- 6
- 74
- 97
-
sizeof returns the size with respect to the size of the `char`. The `char` type usually is 8 bit long, but doesn't have to be. I'm not saying I know such a system, but it's not impossible to have a system, where `char` is 32 bit long and in such a system `sizeof(char)`, `sizeof(int)` and `sizeof(void*)` all can be equal to 1. – Maciej Hehl Oct 04 '10 at 15:34
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?

- 5,100
- 5
- 32
- 50
Size of a Pointer is equal to the size of an Integer . It can be 2 bytes in a 16bit compiler and 4 bytes in 32 bit compiler and 8 in 64 bit compiler.
void *ptr
, int *ptr
and char *ptr
will give you same size but if you do ptr++
, the corresponding pointer will jump according to there data types. ie 1 position in void
and char
case. Similarly 4 positions in int
case.

- 47,830
- 31
- 106
- 135
-
1Size of a void pointer and size of an int are not the same thing. Using gcc on my OSX machine, `sizeof(int) == 4` and `sizeof(void *) == 8` – Dave C May 22 '20 at 19:29
-
And even before today's 64 bit architectures, many older architectures supported 32 bit pointers for global addressing, while still only having 16 bit `int`s (e.g. sometimes all registers were only 16 bits, so `int`, which is supposed to handle the "fast native integer type" could only be 16 bits, while non-local pointer addresses were stored in a pair of 16 bit registers to achieve a 64 bit address space). Some common standards (especially ILP32) make `int` and pointers the same size, but there are [3+ standards which don't](https://unix.org/version2/whatsnew/lp64_wp.html). – ShadowRanger Jan 20 '23 at 16:54