More notes on type equivalence in C 4/29/2004 BGR The examples in lecture Types-3 show how structures are judged to be type equivalent in C. There are 2 ways to view this. 1. C uses structural type equivalence for all types EXCEPT structs and unions. The rules in Types-2 show how to determine structural type equivalence. We think of structs or unions as using name equivalence, EXCEPT that the structural equivalence rule that allows us to rename types (e.g., typedef struct s A;) works for C structs (see lecture notes). 2. We can think that C uses structural equivalence for all types, but when comparing the type constructors for 2 different types, if we encounter the type constructor 'union' or 'struct', then we have to consider those 2 types to be NOT EQUIVALENT. Either perspective 'works' in judging the type equivalence of structs in C. For more examples, look at the C program below: #include int main (void) { int j, n; struct s { int f; } x,y; // redefinition of struct s is illegal //struct s //{ //int f; //} z; typedef struct s t1; //types struct s and t1 are now structurally equivalent typedef struct s t2; struct { int f; }a; struct { int f; } b; typedef struct { int q; } ss; typedef int integer; int a1; integer a2; int *p1; integer *p2; t1 bgr1; t2 bgr2; ss xx,yy; ss zz; bgr1=bgr2; //types t1 and t2 are both structurally equivalent to struct s x = y; //both type struct s zz = xx; //both type ss yy = xx; //both type ss a1=a2; //structurally equivalent types becuase int and integer are equiv p1=&a1; //address of int can be stored in pointer to integer, since //int and integer are equiv types p2=p1; //structurally equivalent types; int equiv integer; Ptr() type //type construtor applied to both. // a = b; incompatible types in assignment }