Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.std.c Subject: Re: Why no arithmetic on void * Message-ID: <561@taumet.com> Date: 22 Jan 91 17:25:29 GMT References: <1238@dkunix9.dk.oracle.com> Organization: Taumetric Corporation, San Diego Lines: 28 bengsig@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@taumet.com