Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!know!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!pilchuck!dataio!gtenmc!ravim From: ravim@gtenmc.UUCP (Ravi K Mandava ( Vox Populi )) Newsgroups: comp.lang.c,tdcs.general Subject: Handling of untagged structures by the C compiler Keywords: structures, equivalence, tagged, typecast Message-ID: <879@gtenmc.UUCP> Date: 20 Sep 90 21:57:37 GMT Reply-To: ravim@gtenmc.UUCP (Ravi Kumar Mandava) Followup-To: comp.lang.c Distribution: na Organization: GTE Telecom, Inc. Bothell, WA Lines: 67 The C compiler on our Vax system (with Unix V.3) gives warnings and an error with the source given below. 1 #include 2 #include 3 4 #define ST struct { int i; } 5 6 main() 7 { 8 ST *a; 9 ST *b; 10 ST c, d; 11 12 if ((a = (ST *)malloc(sizeof(ST))) == NULL) Warning: illegal structure pointer combination 13 { 14 perror("MALLOC"); 15 exit(-1); 16 } 17 18 b = a; Warning: illegal structure pointer combination 19 c = *a; Error: assignment of different structures 20 d = c; 21 } It appears that for every untagged structure declaration/typecast, the compiler generates a unique tag internally and does not bother to check for equivalence of the previously encountered structures. Why can't the compilers be made more intelligent so as to recognize the equivalence of structure types with the same definitions and handle the untagged structures properly (as they do in the case of typedefs and tagged declarations)? For this problem, the error at line 19 and warning at 18 (where variables are assigned) can be resolved by changing the declaration statement to ST *a, *b, c, d; But the warning at line 12 (where typecast is specified) can not be resolved unless a typedef or a tag is used consistently for the declarations and typecasts. Isn't this imposing a restriction on using untagged declarations? IMHO, the compilers should atleast resolve the typecast equivalence problem (so that one can do with untagged structures for all practical purposes). [As for myself, I always try to use typedefs or tags for structures. I happened to come across this problem while bug-fixing someone else's code. So please no chidings for writing bad code :-) ] Or is it that this limitation is only with this particular compiler? Thanks. - Ravi Mandava