Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!haven!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Do you have to cast void pointers when dereferencing them? Keywords: (With ANSI C) Message-ID: <15012@mimsy.UUCP> Date: 15 Dec 88 01:02:23 GMT References: <2414@ssc-vax.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 25 In article <2414@ssc-vax.UUCP> dmg@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@mimsy.umd.edu Path: uunet!mimsy!chris