Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!ucselx!bionet!agate!darkstar!terra!daniel From: daniel@terra.ucsc.edu (Daniel Edelson) Newsgroups: comp.lang.c++ Subject: Re: pointer arithmitic on a void* Keywords: pointer arithmatic void* Message-ID: <6746@darkstar.ucsc.edu> Date: 10 Sep 90 18:05:13 GMT References: <1990Sep7.144048.2049@gli.com> Sender: usenet@darkstar.ucsc.edu Reply-To: daniel@cis.ucsc.edu (Daniel Edelson) Distribution: comp.std.c Organization: University of California, Santa Cruz Lines: 45 In article <1990Sep7.144048.2049@gli.com> bobj@gli.com (Robert Jacobs) writes: > >Is the following syntax allowable in c++. The reason I ask is because a version >of my compiler used to allow it but the UPGRADE forbids it. > >int foo(void *p,int width) >{ > void *q; > q = p+width; >} > >The compiler complains about not knowing the size information. >In order to get this to work I have to recast p to either an int or a char*. >Note I am not trying to dereference a void. I know that is illegal. >If pointer arithmitic is not allowed on a void* then its usefullness is much >reduced. >Robert Jacobs >bobj@gli.com Pointer arithmetic on void * is illegal. This does not reduce the usefullness of the void * type because arithmetic on void * would mean ``add an unknown quantity to this pointer.'' According to the ARM section 5.7 additive operators can only be applied to a pointer when the pointer points to an array element. This is the only legal pointer arithmetic. According to 8.2.4 an array may be constructed from any built-in type except void. Therefore no pointer arithmetic with void* is legal. This must be the case because there's no way a void* pointer could be correctly incremented. At the raw machine level, what value should the compiler add to the pointer value? If the pointer pointed at an integer, p+1 would add sizeof(int) to the pointer. If it pointed at a char then sizeof(char) would be added. In general, because of how K&R first defined pointer arithmetic the size of the referenced object must be known for a pointer to be arithmetiked Daniel Edelson | Don't just garbage collect, recycle. daniel@cis.ucsc.edu, or, | ...!sun!practic!peren!daniel |