Hello,
I think I'm confused with relation between 'pointer' and CHAR_BIT value. Is
there any at all? Specifically, when talking about pointer's size, why do we
have to consider byte size? I understand that pointers are used with regard
to how many bytes of memory they can address, but what is direct relation
with bytes and as such with CHAR_BIT ?
You may have assembled fragments of different discussions into
one amalgamated misunderstanding ;-)
Bear in mind that the size of a pointer and the size of the thing
it points at are independent, and may well be different. An `int*'
might occupy 8 bytes while an `int' occupies 4, or a `double*' might
occupy 4 bytes while a `double' uses 8. A `char*' points at exactly
one byte (there may be others nearby), but the pointer itself quite
likely occupies more than one.
If you know the number of bytes a certain pointer type occupies,
you can multiply that quantity by CHAR_BIT to find the number of
bits in the pointer's value. Raise 2 to that bit count and you get
an upper bound on the number of distinct values the pointer could
possibly have. It's not a tight bound, though, because not all
those values need be valid: On systems where a `double' requires
8-byte alignment, it's quite likely that at least 3 bits of a
`double*' are "must be zero" -- and there may be other reserved
bits, too.
Finally, be aware that different pointer types may need
different amounts of storage. On many systems all pointers of
all kinds are represented the same way (and thus use the same
amount of memory), but this is not universal and C does not
require it. On some systems, pointers to functions look quite
different from pointers to data. A few systems use different
representations for pointers to different data types (a word-
addressed machine, for example, might use a plain address to
represent an `int*', but might need an address plus a byte
number to point at a particular `char' inside a word). So on
some systems you might find `sizeof(char*) != sizeof(int*)',
and if you plan to malloc() space to hold a bunch of pointers
you need to pay attention to their possibly different sizes.