Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!think.com!barmar From: barmar@think.com (Barry Margolin) Newsgroups: comp.lang.c++ Subject: Re: char* vs void* Keywords: char* vs void* pointer arithmitic Message-ID: <1991Jan4.063649.6464@Think.COM> Date: 4 Jan 91 06:36:49 GMT References: <277BAC53.F2A@tct.uucp> <1991Jan2.171429.11566@glinj.gli.com> <11223@cae780.csi.com> Sender: news@Think.COM Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 36 In article <11223@cae780.csi.com> donald@cae780.UUCP (Donald Maffly) writes: >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? Well, they could have defined it in terms of the units that sizeof measures in. For instance, if you have typedef ... foo_struct; foo_struct[N] foo; void *vp; foo_struct *fp; fp = &foo_struct[0]; vp = (void *) fp; vp += sizeof foo_struct; fp = (foo_struct *) vp; At this point, fp == &foo[1] . This currently works if you replace "void" with "char", because sizeof char is required to be 1. They could have specified that sizeof void must also be 1, and then all the uses of "char" as the generic basic memory object could be abandoned. This would pave the way for future revisions of the C standard to allow sizeof char > 1 (e.g. to get rid of the char/w_char distinction). -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar