Handling of untagged structures by the C compiler

Al Dunbar userAKDU at mts.ucs.UAlberta.CA
Sat Sep 22 15:31:54 AEST 1990


In article <879 at gtenmc.UUCP>, ravim at gtenmc.UUCP (Ravi K Mandava ( Vox Populi )) writes:
>The C compiler on our Vax system (with Unix V.3) gives warnings and an error
>with the source given below.
> ....
>
>   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)?
>
 
It's all a matter of your point of view. Does the compiler treat
the following as an error?
 
struct ST1 {int i;} v1;
struct ST2 {int i;} v2;
v1 = v2;
 
It should, as the intent is obviously that v1 and v2 are two
different kinds of things. The fact that the definition of ST1
and ST2 are identical is merely a coincidence. If you want v1
and v2 to be of the same type, you have a number of options:
 
a) struct ST1 {int i;};   /* with tags */
struct ST1 v1;
struct ST1 v2;
 
b) struct ST1 {int i;} v1, v2;   /* with tags */
 
or:
 
c) struct {int i;} v1, v2;     /* without tags */
 
But if:
 
d) struct {int i;} v1;
struct {int i;} v2;
 
is taken to mean that v1 and v2 are of the same type simply
because their definitions are identical, how would
it be possible to get the compiler to distinguish between
them in those cases where that is what you want?
 
An analogy might help here. Supposing that the "type" of
struct variables as determined by the compiler is a pointer
(in the sense of an address) to the structure definition.
The v1 and v2 defined by (d) would obviously be of different
types, while those of examples (a), (b), and (c) would
be identical, as the tag would be like a pointer constant.
 
-------------------+-------------------------------------------
Al Dunbar          |
Edmonton, Alberta  |   this space for rent
CANADA             |
-------------------+-------------------------------------------



More information about the Comp.lang.c mailing list