Is the size of the datatype "int" always equals to the size of a pointer in the c language?
I'm just curious.
Not at all, there is no guarantee that sizeof(int) == sizeof(void*)
. And on Linux/AMD64 sizeof(int)
is 4 bytes, and sizeof(void*)
is 8 bytes (same as sizeof(long)
on that platform).
Recent C standard (e.g. C99) defines a standard header <stdint.h>
which should define, among others, an integral type intptr_t
which is guaranteed to have the size of pointers (and probably even which is reversably castable to and from pointers).
I think that the standard does not guarantee that all pointers have the same size, in particular pointer to functions can be "bigger" than data pointers (I cannot name a platform where it is true). I believe that recent Posix standard requires that (e.g. for dlsym
(3)).
See also this C reference and the n1570 draft C11 standard (or better)
PS. In 2021 I cannot name a common platform with sizeof(long) != sizeof(void*)
. But in the previous century the old intel 286 could have been such a platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With