Why no arithmetic on void *

Doug Gwyn gwyn at smoke.brl.mil
Thu Jan 24 10:27:53 AEST 1991


In article <1238 at dkunix9.dk.oracle.com> bengsig at dk.oracle.com (Bjorn Engsig) writes:
>Could someone please explain if arithmetic on void * (with the same semantics
>as on char *) was discussed when ANSI C was made, and why it was not included.

The main purpose of void and void* was to provide additional compile-time
safety checking.  This would be largely defeated if pointer arithmetic
had been allowed on void*.  Note that such arithmetic would have had to
have been specially defined for that one type, since it does not fit the
pointer-arithmetic model given for additive operators.  (If it HAD been
made consistent, we would have had to declare that sizeof(void)==0 and
then void* arithmetic would not have had the same behavior as char*
arithmetic anyway.  So it would have had to violate the model, which is
obviously extremely undesirable.)

Note also that there is absolutely no need for such a wart, since you
can perform the desired pointer arithmetic after casting the void* to
char*.



More information about the Comp.std.c mailing list