Why no arithmetic on void *

Chip Salzenberg chip at tct.uucp
Thu Jan 24 00:46:58 AEST 1991


According to barmar at think.com (Barry Margolin):
>However, it would be nice if void* could also be used as a way to pass
>pointers to an array of arbitrary objects.

Of course it can.

Aside: In normal usage, what we call "pointer to an array" is usually
"pointer to the first element of the array."  C _allows_ a declaration
of a genuine "pointer to an array," but such a pointer is typically no
more useful than a pointer to the first element.

In any case, a |void*| can represent all of the above pointer types.

>It seems to me that ANSI C only goes halfway in providing a generic
>pointer, because generic pointer arithmetic must be done by casting
>to a non-generic type, namely char*.

That's one interpretation.  I see it as leveraging off existing
practice.  Remember that |void*| and |char*| must have identical
representations and that sizeof(char) must always equal one.  So
|char*| is just as generic a pointer as |void*|.

Given these facts, the utility of |void*| lay not in its similarities
to |char*|, but in its differences: conversions without casts and no
allowance for arithmetic.

>Future C standards might then be able to get rid of the notion that char
>is the fundamental unit ...

I see what you're driving at -- but such a language wouldn't be C any
more.  Many existing programs would break.  The assumption
"sizeof(char)==1" is hard-coded into innumerable calls to fread() and
other library functions.
-- 

Chip Salzenberg at Teltronics/TCT     <chip at tct.uucp>, <uunet!pdn!tct!chip>
          perl -e 'sub do { print "extinct!\n"; }   do do()'



More information about the Comp.std.c mailing list