char* vs void*
Donald Maffly
donald at cae780.csi.com
Fri Jan 4 11:33:00 AEST 1991
In article <1991Jan2.171429.11566 at glinj.gli.com> bobj at glinj.gli.com (Robert Jacobs) writes:
>
>>Pointers of types |char *| and |void *| must both be able to hold the
>>address of any object. The reason why this is so differs for the two
>>types. For |char *| it's existing practice, and for |void *| it's the
>>decree of ANSI.
>>
>> [ ... ]
>>
>
>What I can't understand is that ANSI decided that one cannot do pointer
>arithmitic on a void*, like a char*. Because of this, new work is forced
>to use char* as the generic pointer that can do pointer arithmitic.
>This continues the existing practice that char* is special. Void*
>should be allowed to do pointer arithmitic so that char* does not
>have to be used for new work.
Why did ANSI decide that one cannot do pointer arithmetic on a void*
pointer, you ask.
Well, BY DEFINITION, void* means that
we are dealing with a pointer to an object of unknown type and size.
In pointer arithmetic, how could a C compiler possibly know how much
to decrement or increment a pointer by if it didn't know the size of
the object it was pointing to?
(rhetorical).
For example, if we had a dynamically allocated array of objects of
unknown size, and "ptr" of type (void*) points to some object in the array,
what sense would "ptr++" make? Surely, the intent is to reset the
pointer to point to the next object in the array. But without the
contextual knowledge of the size of each object, the expression "ptr++"
is not well defined.
I haven't been following this char* vs. void* discussion very closely,
so excuse me if I have missed something crucial and I am way off base.
Donald Maffly
More information about the Comp.lang.c
mailing list