Why no arithmetic on void *
    Stephen Clamage 
    steve at taumet.com
       
    Wed Jan 23 04:25:29 AEST 1991
    
    
  
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.
If you want the semantics of char*, you should declare a char*.
Void* was an invention of the Committee to cover the case of "pointer to
something of unknown type".  This allows a guaranteed portable way to
pass pointers to an arbitrary object to a routine which can make use of
such a thing, and a way to store pointers to an arbitrary object.
When you add/subtract 1 to/from a pointer, the pointer is incremented/
decremented by the size of the object it points to.  Since the size of the
object pointed to by a void* is by definition unknown, pointer arithmetic
is illegal.
Example: If you have
	void *p = &mydata;
why would you want the expression (p+1) to point one char past the start
of mydata?  If for some reason that is what you want, you can write
((char*)p+1).  Or if you really want to do pointer arithmetic with p
without a lot of casts, you can write
	char  *p = (char*)&mydata;
-- 
Steve Clamage, TauMetric Corp, steve at taumet.com
    
    
More information about the Comp.std.c
mailing list