Next: , Previous: , Up: Top   [Contents][Index]


23 Compatible Types

Declaring a function or variable twice is valid in C only if the two declarations specify compatible types. In addition, some operations on pointers require operands to have compatible target types.

In C, two different primitive types are never compatible. Likewise for the defined types struct, union and enum: two separately defined types are incompatible unless they are defined exactly the same way.

However, there are a few cases where different types can be compatible:

In order for types to be compatible, they must agree in their type qualifiers. Thus, const int and int are incompatible. It follows that const int * and int * are incompatible too (they are pointers to types that are not compatible).

If two types are compatible ignoring the qualifiers, we call them nearly compatible. (If they are array types, we ignore qualifiers on the element types.7) Comparison of pointers is valid if the pointers’ target types are nearly compatible. Likewise, the two branches of a conditional expression may be pointers to nearly compatible target types.

If two types are compatible ignoring the qualifiers, and the first type has all the qualifiers of the second type, we say the first is upward compatible with the second. Assignment of pointers requires the assigned pointer’s target type to be upward compatible with the right operand (the new value)’s target type.


Footnotes

(7)

This is a GNU C extension.


Next: , Previous: , Up: Top   [Contents][Index]