Do you have to cast void pointers when dereferencing them?
Chris Torek
chris at mimsy.UUCP
Thu Dec 15 11:02:23 AEST 1988
In article <2414 at ssc-vax.UUCP> dmg at ssc-vax.UUCP (David Geary) writes:
[edited]
>struct junk { int x,y; };
> struct junk J;
> void *p;
> p = &J;
> ... p->x ...
The assignment `p = &J' (uncast) is legal dpANS C. The reference
`p->x' is not. To see why, consider this fragment:
struct foople { char alpha, omega; } f;
struct gluxet { int rho, omega; } g;
void *p;
if (flipcoin() == HEADS) p = &f; else p = &g;
printf("%d\n", p->omega);
Which omega should be used?
(I used `if ... p = &f; else p = &g;' instead of `p = ... ? &f : &g;'
to avoid questions about mixing ?: pointer types, which, when last
I checked, seemed rather muddled. The sensible approach is to make
`... ? &f : &g' illegal.)
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list