🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2309

Re: Why no arithmetic on void *

#2309
From: Stephen Clamage
Date: Tue, 22 Jan 1991 17:25
28 lines
1543 bytes
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
Technical Details
Message-ID: <561@taumet.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!taumet!steve
References: <1238@dkunix9.dk.oracle.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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