Handling of untagged structures by the C compiler
Ravi K Mandava Vox Populi
ravim at gtenmc.UUCP
Fri Sep 21 07:57:37 AEST 1990
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
More information about the Comp.lang.c
mailing list