Presentation is loading. Please wait.

Presentation is loading. Please wait.

Type compatibility and pointer operation

Similar presentations


Presentation on theme: "Type compatibility and pointer operation"— Presentation transcript:

1 Type compatibility and pointer operation
Kei Hasegawa

2 Type compatibility Refer to ISO IEC C98/C99 for restrict definition. Roughly say, Type T1 is compatible with type T2 <-> extern T1 x; extern T2 x; is not error.

3 Array compatibility typedef int T1[]; typedef int T2[10]; extern T1 x;
is not error. So, Type int [] is compatible with type int [10]

4 Function compatibility
typedef void T1(int (*)(int), double (*)[]); typedef void T2(int (*)(), double (*)[2]); typedef void T3(); extern T1 x; extern T2 x; extern T3 x; is not error. So, Type void (int (*)(int), double (*)[]), type void (int (*)(), double (*)[2]) and type void () are compatible with for each other.

5 Tagged type compatibility
struct S; extern struct S x; // incomplete type struct S { int a; }; extern struct S x; // complete type is not error. So, incomplete struct S is compatible with complete struct S.

6 Pointer compatibility
Type T1 is compatible with type T2 <-> extern T1 *x; extern T2 *x; is not error.

7 Canonical rule of pointer operation
Pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. For example, int* p and const int* q shall have the same representation and alignment requirements. So that’s why comparison int* p and const int* q or assignment like q = p are valid. But p = q is error in sense of discarding qualifier. For int** p2 and const int** q2, any operation is not defined. i.e. it’s error. Type of p2 is pointer to int* and type of q2 is pointer to const int*, but int* is not compatible with const int*.

8 Pointer and pointer operation – applied canonical rule
p – q p < q, p > q, p <= q, p >= q p == q, p != q possible to compare void* and 0 expr ? p : q can be void* or 0 p = q


Download ppt "Type compatibility and pointer operation"

Similar presentations


Ads by Google