Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler of c type equivalence

Tags:

c

In C language, if we have:

typedef int a[100];
typedef int b[200];

Then types a and b are equivalent? As far as I know C uses name equivalence, but I'm very confused.

like image 531
DIMITRIOS Avatar asked Jun 01 '17 15:06

DIMITRIOS


1 Answers

In your example a is alias for the type int[100] and b is an alias for the type int[200]. These two types are not equivalent as an array's size is part of its type and different sizes mean different types.

If the second line were typedef int b[100];, a and b would be equivalent.

As far as i know C uses name equivalence

Structs and unions are nominally typed in the sense that if you have two struct (or union) types tagged A and B, which have exactly the same body, they're still considered different types. So if a variable has type struct A, you can't assign it a struct B without converting it first.

This is not true for typedefs, which are mere aliases. So if you have two typedefs typedef X a; typedef X b;, then X, a and b are all indistinguishable from each other.

like image 188
sepp2k Avatar answered Sep 28 '22 18:09

sepp2k