[comp.lang.c] Handling of untagged structures by the C compiler

ravim@gtenmc.UUCP (Ravi K Mandava ( Vox Populi )) (09/21/90)

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

scjones@thor.UUCP (Larry Jones) (09/21/90)

In article <879@gtenmc.UUCP>, ravim@gtenmc.UUCP (Ravi K Mandava ( Vox Populi )) writes:
>    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.

Exactly.  That is how C is supposed to work according to the ANSI standard.
If you want identical structures you have to use a tag or a typedef.
----
Larry Jones                         UUCP: uunet!sdrc!thor!scjones
SDRC                                      scjones@thor.UUCP
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
Your bangs do a good job of covering up the lobotomy stitches. -- Calvin

karl@haddock.ima.isc.com (Karl Heuer) (09/22/90)

In article <879@gtenmc.UUCP> ravim@gtenmc.UUCP (Ravi Kumar Mandava) writes:
>	#define ST struct { int i; }
>[lost of stuff refering to `ST *']
>Why can't the compilers be made more intelligent so as to recognize the
>equivalence of structure types with the same definitions...?

I have a struct complex, which consists of two numbers, and a struct vec2d,
which consists of two numbers.  They are logically distinct types, and I want
the compiler to complain if I mix them.  Your proposal would prevent this.

A better idea is to educate the user to quit doing that nonsense.  There's no
good reason for that symbol to be a macro instead of a typedef.

Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint

userAKDU@mts.ucs.UAlberta.CA (Al Dunbar) (09/22/90)

In article <879@gtenmc.UUCP>, ravim@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             |
-------------------+-------------------------------------------

kaiser@ananke.stgt.sub.org (Andreas Kaiser) (09/22/90)

In a message of <Sep 20 21:57>,  Vox Populi ) (ravim@gtenmc.UUCP (Ravi K Mandava ) writes: 

 VP)>     4   #define ST struct { int i; }
 VP)>     8           ST *a;
 VP)>    12           if ((a = (ST *)malloc(sizeof(ST))) == NULL)

 VP)>    It appears that for every untagged structure declaration/typecast, the
 VP)>    compiler generates a unique tag internally and does not bother to
 VP)>    check for equivalence of the previously encountered structures.

According to ANSI, two structures are only compatible, if they have the same 
"tagged type". Two identically declared structures are incompatible.

 VP)>    [As for myself, I always try to use typedefs or tags for structures.
 VP)>     I happened to come across this problem while bug-fixing someone
 VP)>     else's code.  So please no chidings for writing bad code :-) ]

 VP)>    Or is it that this limitation is only with this particular compiler?

Some very old C compilers (the original Ritchie compiler for PDP-11) do not care 
about structure types at all. It was possible to write code like this (found in 
UNIX Version 6):

        register int i;
        struct { char high, low; };
        .... i.high ....

 

--  
:::::::::::::::::::: Internet: kaiser@ananke.stgt.sub.org
:: Andreas Kaiser :: Fidonet:  2:507/18.7206 (+ 2:509/5.2512)
::::::::::::::::::::