Xref: utzoo comp.lang.c++:10979 comp.lang.c:35013 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!ucsd!ames!cae780!donald From: donald@cae780.csi.com (Donald Maffly) Newsgroups: comp.lang.c++,comp.lang.c Subject: Re: char* vs void* Keywords: char* vs void* pointer arithmitic Message-ID: <11223@cae780.csi.com> Date: 4 Jan 91 00:33:00 GMT References: <11745@alice.att.com> <2778A795.6E71@tct.uucp> <17590@paperboy.OSF.ORG> <277BAC53.F2A@tct.uucp> <1991Jan2.171429.11566@glinj.gli.com> Reply-To: donald@cae780.UUCP (Donald Maffly) Followup-To: comp.lang.c++ Organization: Comdisco Systems Inc. Foster City, CA. Lines: 39 In article <1991Jan2.171429.11566@glinj.gli.com> bobj@glinj.gli.com (Robert Jacobs) writes: > >>Pointers of types |char *| and |void *| must both be able to hold the >>address of any object. The reason why this is so differs for the two >>types. For |char *| it's existing practice, and for |void *| it's the >>decree of ANSI. >> >> [ ... ] >> > >What I can't understand is that ANSI decided that one cannot do pointer >arithmitic on a void*, like a char*. Because of this, new work is forced >to use char* as the generic pointer that can do pointer arithmitic. >This continues the existing practice that char* is special. Void* >should be allowed to do pointer arithmitic so that char* does not >have to be used for new work. Why did ANSI decide that one cannot do pointer arithmetic on a void* pointer, you ask. Well, BY DEFINITION, void* means that we are dealing with a pointer to an object of unknown type and size. In pointer arithmetic, how could a C compiler possibly know how much to decrement or increment a pointer by if it didn't know the size of the object it was pointing to? (rhetorical). For example, if we had a dynamically allocated array of objects of unknown size, and "ptr" of type (void*) points to some object in the array, what sense would "ptr++" make? Surely, the intent is to reset the pointer to point to the next object in the array. But without the contextual knowledge of the size of each object, the expression "ptr++" is not well defined. I haven't been following this char* vs. void* discussion very closely, so excuse me if I have missed something crucial and I am way off base. Donald Maffly