K
Keith Thompson
blargg said:Andrew Tomazos wrote: [...]With the strict solution we have to cast to some other pointer type
before doing pointer arithmetic.
In the looser solution we don't have to cast, but there is a small
chance of accidentally doing pointer arithmetic by accident, or in the
wrong unit. This will not be caught until runtime in this case.
The looser solution also breaks consistency. In C, adding n to a T*
equivalently adds n*sizeof(T) to a char*-casted version.
gcc, with extensions enabled, has sizeof(void) == 1. Presumably this
is a side effect of allowing pointer arithmetic on void*.
gcc also allows pointer arithmetic on function pointers -- and yes,
sizeof applied to a function type yields 1. For example,
sizeofmain == 1.
Along with allowing arithmetic as if it were a char*, why not also allow
dereferencing? Then you're left with a type that behaves just like
char*, except it also allows implicit conversion to/from other pointer
types.
At least gcc doesn't go so far as to allow dereferencing void*.
(Actually it does allow it, with a warning, as long as the result
isn't used.)