[comp.lang.c] arithmetic on void pointers

russ@groucho.ucar.edu (Russ Rew) (03/07/89)

GNU C supports arithmetic on void pointers, but the manual implies
that this is an extension to the C language.  I could find no
prohibition on arithmetic on void pointers in K&R second edition.
What does the latest dpANS document say?  Is it legal to increment a
variable of type void* ?

--
Russ Rew                                     UCAR, P.O. Box 3000          
INTERNET: russ@unidata.UCAR.EDU		     Boulder, CO  80307
UUCP: russ@scdpyr.UUCP                       (303) 497-8845

gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/07/89)

In article <1527@ncar.ucar.edu> russ@groucho.UUCP (Russ Rew) writes:
>Is it legal to increment a variable of type void* ?

Of course not, because there are no void objects and therefore no way
to point at the "next" one.

friedl@vsi.COM (Stephen J. Friedl) (03/07/89)

In article <1527@ncar.ucar.edu>, russ@groucho.ucar.edu (Russ Rew) writes:
> What does the latest dpANS document say?  Is it legal
> to increment a variable of type void* ?

No.

When incrementing a pointer, one multiplies the increment by the
size of the pointed-to object.  Since a `void' has no size, you
can't get to the next one.

The dpANS does not directly say "You can't ++ a void*", so to prove
it you need to string a couple of parts together.

From the May88 dpANS:  (renditions: ITALICS, /Courier/)

	3.1.2.5 - Types

		"The /void/ type comprises an empty set of values; it
		is an incomplete type that cannot be completed"

### /void/ is an incomplete type

	3.3.3.4 - The /sizeof/ operator

		"The /sizeof/ operator shall not be applied to an
		expression that has function type or an incomplete
		type [...]"

### you can't take the size of /void/

	3.3.2.4 - Postfix increment and decrement operators
and	3.3.3.1 - Prefix increment and decrement operators

		"The operand of the prefix increment or decrement
		operator shall have qualified or unqualified scalar
		type and shall be a modifiable lvalue."

### you can't ++ or -- a /void/



     I would guess that those implementations that permit ++ on
void pointers do so by treating them as char pointers instead.

     Steve, the staggering dpANS (with apologies to Karl)

-- 
Stephen J. Friedl / V-Systems, Inc. / Santa Ana, CA / +1 714 545 6442 
3B2-kind-of-guy   / friedl@vsi.com  / {attmail, uunet, etc}!vsi!friedl

"Cold beer, hot women, and fast compilers" - me

bill@twwells.uucp (T. William Wells) (03/07/89)

In article <1527@ncar.ucar.edu> russ@groucho.UUCP (Russ Rew) writes:
: GNU C supports arithmetic on void pointers, but the manual implies
: that this is an extension to the C language.  I could find no
: prohibition on arithmetic on void pointers in K&R second edition.
: What does the latest dpANS document say?  Is it legal to increment a
: variable of type void* ?

When the dpANS talks about pointer arithmetic, it speaks of pointers
to an object type.  Void is an incomplete type, a pointer to void is
thus not a pointer to an object type, so a void pointer can't be used
in pointer arithmetic.

---
Bill
{ uunet | novavax } !twwells!bill
(BTW, I'm going to be looking for a new job sometime in the next
few months.  If you know of a good one, do send me e-mail.)

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (03/08/89)

In article <1527@ncar.ucar.edu> russ@groucho.UUCP (Russ Rew) writes:
| GNU C supports arithmetic on void pointers, but the manual implies
| that this is an extension to the C language.  I could find no
| prohibition on arithmetic on void pointers in K&R second edition.
| What does the latest dpANS document say?  Is it legal to increment a
| variable of type void* ?

  Let's see... a pointer increment on a char pointer adds sizeof(char)
to the pointer, increment on int pointer adds sizeof(int), I guess you
would conclude that increment on a void pointer would add sizeof(void).
I *think* dpANS says sizeof(void) is zero (I don't have it here, so
correct me if you can find the section).

  I conclude that if it was allowed it wouldn't do anything. I don't
think it's allowed.

One of my C students handed in this code fragment:
	void *foo, *getsym();
	int m;
	
	foo = getsym(22);
	m = (int) foo[4];

I *think* he meant "m = ((int *) foo)[4]" which seems legal if not the
clearest way to do it.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/08/89)

In article <13323@steinmetz.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes:
>I *think* dpANS says sizeof(void) is zero

No, it's illegal.

C as it stands now has no zero-sized objects.

There is an unofficial "extensions working group" to investigate
ways to incorporate zero-sized objects into C, but as its point
of contact I must say there has been remarkably little interest
in the subject.