Article View: tdcs.general
Article #1Handling of untagged structures by the C compiler
From: Ravi K Mandava ( Vox Populi )
Date: Thu, 20 Sep 1990 21:57
Date: Thu, 20 Sep 1990 21:57
68 lines
2797 bytes
2797 bytes
The C compiler on our Vax system (with Unix V.3) gives warnings and an error
with the source given below.
1 #include <stdio.h>
2 #include <malloc.h>
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
Technical Details
Message-ID:
<879@gtenmc.UUCP>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp!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!ravimShow Full Headers
Complete Article Headers
Message-ID: <879@gtenmc.UUCP> Subject: Handling of untagged structures by the C compiler Date: Thu, 20 Sep 1990 21:57:37 +0000 From: ravim@gtenmc.UUCP (Ravi K Mandava ( Vox Populi )) Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp!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 Newsgroups: comp.lang.c,tdcs.general Keywords: structures, equivalence, tagged, typecast Reply-To: ravim@gtenmc.UUCP (Ravi Kumar Mandava) Followup-To: comp.lang.c Distribution: na Organization: GTE Telecom, Inc. Bothell, WA Lines: 67