Type declarations: typedef struct { int i; double val; } test; typedef int num;
ID: 3796532 • Letter: T
Question
Type declarations:
typedef struct { int i; double val; } test;
typedef int num; typedef double value;
typedef struct { int x; double y; } mystruct;
typedef struct { num i; value val; } myrec;
typedef struct { int x; double val; } test2;
typedef struct { int q; } istruct;
typedef struct { double r; } dstruct;
typedef struct { istruct i; dstruct d; } tests;
typedef test test3
Variable declarations:
test p,q;
mystruct t,u;
myrec s,r;
test2 m,n; tests c,d;
test3 h,g;
a) If we use structural equivalence for all type comparisons, which of these variables will be of the same types?
b) If we use strict name equivalence for all type comparisons, which of these variables will be of the same types?
c) If we use declaration name equivalence for all type comparisons, which of these variables will be of the same types?
d) Verify that the language C uses declaration equivalence.
Your examples (you may attach code) should clearly indicate that the compiler does not allow structurally equivalent variables to be treated as type-compatible. It should also clearly indicate that the language C use declaration equivalence, not the more stringent name equivalence.
Explanation / Answer
Solution:
a) If we use structural equivalence for all type comparisons, then the variab le that will be pof the same types are p,q,t,u,m,n,c,d,h,g,s,r. All these are considered to be structurally equivalent because according to the definition the two types are considered to be equal iff they contains the same structure, which may have different ways to be interpreted. All the above variables have different interpretations but the structure thay are having is all same.
b) When we talk about strict name equivalence, it is defined as; two types are said to be equal iff they have same name for eg.
typedef struct {
int arr[10];
int cnt;
} Stack;
typedef struct
{
int arr[10];
int cnt;
} Set;
Stack a,b;
Set c,d;
As per name equvalence a is equal to b and c is equal to d.
According to the question asked, the variables that follow name equvalence are:
p and q
t and u
m and n
c and d
h and g
c) Variables are name type equivalent because they have the same type by name.Also, name equivalence depends on actual occurrences of declarations in the source code. Like ,
int a,b;
they both follow declaration name equivalence because they have same type by name.
Now, in the above example the variables that follow declaration name equivalence are
p and q
t and u
m and n
d) C uses declaration equilencenot the more stringent name equivalence can be understood by the example given below:
float a;
float b;
when we declare like this, both a and b will have the same declaration name equivalent i.e. float. and now if we declare this as
float a,b;
this again means the same thing that both a and b are declaration name equivalent i.e. float.
There is no difference in float a; float b; float a,b; as all of them indicates the same type (structurally and by name). This is all valid in C language and gives no error.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.