Suppose I have:
int a;
int b;
Are the variables a
and b
name equivalent (more specifically, since primitive types don't have type names, can they be considered name equivalent)?
Thanks.
Suppose I have:
int a;
int b;
Are the variables a
and b
name equivalent (more specifically, since primitive types don't have type names, can they be considered name equivalent)?
Thanks.
C adopts structural equivalence except for structures and unions; for these types, name equivalence is used. It is true that the values have the same type as determined by the name of their type in name equivalence and that the values are considered to have the same type as determined by structurally equivalent in structural equivalence. But even in structural equivalence, it is not determined regardless of the name.
Basically, atomic types in a programming language have names and they are used both in name equivalence and structural equivalence. The difference comes from derived types such as int *
or char[10]
. It is true that "primitive types have type names." And C's type system is comparing the structure of derived types up to user-defined names excluding type synonyms even they are defined by the user using typedef
.
C's type system is generally adopting structural equivalence. For instance, int *
and short *
are different types but int *
and int *
are the same types even though no names are attributed to the pointers. The reason for that struct foo { int x; }
and struct bar { int x; }
are different types is because they have different structure up to user-defined type names. Types struct foo *
and struct foo *
are the same types since they have the same structure. Note that struct
defines not only the structure but also name, more precisely structure tag name, of the type.
Simply, in structural equivalence, the types are unfolded up to atomic type names and they are compared. In C, the types are unfolded up to names including user-defined names by struct
or union
. (For unfolding, please refer http://www.csd.uwo.ca/~moreno//CS447/Lectures/TypeChecking.html/node3.html) This is the difference. Naturally, name equivalent types are structurally equivalent, but not the converse.
For the following case:
int x;
int y;
x
and y
and both name equivalent and structural equivalent. But, in the following case:
int *x;
int *y;
x
and y
are structural equivalent but not name equivalent. Even for the following case:
int *x, *y;
x
and y
are not considered equivalent in a strict name equivalence scheme, for this declaration is considered as just a shorthand of the previous declaration. Ada is a well-known language adopting the strict name equivalence.
Name (more properly, nominal) equivalence means that the values have the same type as determined by the (fully qualified) name of their type -- for instance,
a
andb
are nominally type equivalent because they both have "int" type. Structural equivalence means that the values are considered to have the same type because their types are structurally equivalent, regardless of the name. Nominal type equivalence implies structural type equivalence since a named type is structurally equivalent to itself. Youra
andb
, are nominally type equivalent because they have the same type by name ("int"). The claim that "primitive types don't have type names" is simply false --int
is a type name. And there is no difference betweenint a; int b;
andint a, b;
-- both definea
andb
with the same (structurally and by name) type.C's type system is generally by name ... e.g.,
int *
andshort*
are different types even ifint
andshort
have the same representation, andstruct foo { int x; }
andstruct bar { int x; }
are different types even though they always have the same representation.