Path: utzoo!mnetor!uunet!husc6!bloom-beacon!gatech!udel!princeton!phoenix!asjoshi From: asjoshi@phoenix.Princeton.EDU (Amit S. Joshi) Newsgroups: comp.lang.c Subject: Unions and structures in C Message-ID: <1520@phoenix.Princeton.EDU> Date: 22 Jan 88 22:44:42 GMT Organization: Princeton University, NJ Lines: 40 Keywords: C Turbo C Hello, I have a question about unions and structures in C: Suppose I have the following fragment of a program : struct one { float x,y; }; union two { float x[2]; struct one one; }; main () { union two two; two.one.x = 1.0; two.one.y = 2.0; printf(" x = %f, y = %f\n",two.one.x,two.one.y); printf(" x[%d] = %f, x[%d] = %f\n",1,two.x[0],2,two.x[1]); } My question is what would the second printf statement print ? Would I get "x[1] = 1.0, x[2] = 2.0" or "x[1] = 2.0, x[2] = 1.0" or would I get that two.x[?] is undefined ? Or what? Does the order in which the the elements of the structure are present correspond to the other elements declared in the union ? Is any such correspondence guaranteed ? I know from some test cases that in TurboC I seem to get a correspondence between the parts of the union. The reason I need this is that if the order or correspondence were known then I could use very informative names in the structure and use arrays where array processing is more efficient, e.g. suppose x and y were the coordinates of an object then using x and y for code where there is a distinction between the axes is nice whereas when I want to say calculate the motion I might wish to integrate where the array representation is more convinient. Thanks, -- Amit Joshi BITNET | Q3696@PUCC.BITNET USENET | {seismo, rutgers}\!princeton\!phoenix\!asjoshi "There's a pleasure in being mad... which none but madmen know!" - St.Dryden