arithmetic on void pointers

William E. Davidsen Jr davidsen at steinmetz.ge.com
Wed Mar 8 02:58:48 AEST 1989


In article <1527 at ncar.ucar.edu> russ at 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 at ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me



More information about the Comp.lang.c mailing list