Operations on pointers to void.

John Lacey john at basho.uucp
Fri Aug 10 09:16:14 AEST 1990


What operations are legal on pointers to void?  K&R2 is it's usual self
on the matter---no one can tell if what they say is possible, required, 
complete, a couple of examples, or what.  Harbison and Steele are their 
much more explicit selves, but still, all they say is that void *'s can
be assigned to or from without casts, and that dereferencing and 
subscripting are illegal.

But, what about pointer arithmetic?  And it that is legal, what size object
does a void * point to?  If there was justice in the world, I suppose that
it should be legal, and that a void * points to an object of size 0, but
gcc thinks the size is 1 (and gives no complaints about the following code
compiled with -ansi -pedantic -Wall:

#include <stdio.h>

int
main()
{
char foo [] = "This is a test string.";
void * a = foo;
void * b = &foo[1];

printf ( "%s\n", (++a == b) ? "Object size is 1" : "Object size is NOT 1" );
return ( 0 );
}

and further, gives the aforementioned result of "Object size is 1").

Ah what gives?
-- 
John Lacey, 
   E-mail:  ...!osu-cis!n8emr!uncle!basho!john  (coming soon: john at basho.uucp)
   V-mail:  (614) 436--3773, or 487--8570
"What was the name of the dog on Rin-tin-tin?"  --Mickey Rivers, ex-Yankee CF



More information about the Comp.lang.c mailing list