Thread View: tdcs.general
1 messages
1 total messages
Started by Ravi K Mandava ( Vox Populi )
Thu, 20 Sep 1990 21:57
Handling of untagged structures by the C compiler
Author: 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads