Can the size of pointer variables of different type may be
different on a particular architecture.
For eg.
Can the sizeof (char *) be different from sizeof(int *) or
sizeof (void *) ?
Let's say we've got a machine that only adresses memory in 32-bit chunks
(bytes).
One obvious strategy would be to say that chars are 32 bits. This has some
problems. For instance an image-processing routine that takes an array of
unsigned chars as its argument would now gobble four times as much memory as
necessary, or need to be rewritten.
So the alternative is to make chars 8 bit, and do a bit of bit twiddling
behind the scenes to produce the illusion of 8-bit bytes.
But if our addresses are multiples of 32 bits, we need to tag on an extra
two bits to tell us where the pointer points to.
So char pointers become
{
address:
offset:
}
The whole reason the machine accesses data in 32 bit bytes is for
efficiency, so we don't want to encumber our int *s and double *s with
similar tags. That would slow everything down.
so char pointers and int pointers are now a different size.
What is the purpose of using a void pointer ? Instead of
declaring a pointer variable "void *", can I declare it
as "char *" and then later on typcast it to whatever type
as needed.
That was the old-fashioned way of doing things. void * is more for
documentation, to tell the programmer that "this pointer points to memory of
unknown type".
unsigned char * can point to any memory. In practise
double x;
(unsigned char *ptr) = &x;
double *dblptr = (double *) ptr;
will work on pretty much any platform. However it might just be the case
that the double pointers need to carry around an extra bit for some reason,
in which case the code will break. void pointers, of course, are guaranteed
to be able to hold any type.