6 Derived data types
This chapter covers
- Grouping objects into arrays
- Using pointers as opaque types
- Combining objects into structures
- Giving types new names with
typedef
All other data types in C are derived from the basic types that we know now. There are four strategies for deriving data types. Two of them are called aggregate data types because they combine multiple instances of one or several other data types:
- Arrays These combine items that all have the same base type (section 6.1).
- Structures These combine items that may have different base types (section 6.3).
The two other strategies to derive data types are more involved:
- Pointers Entities that refer to an object in memory. Pointers are by far the most involved concept, and we will delay a full discussion of them to section 11. Here, in section 6.2, we will only discuss them as opaque data types, without even mentioning the real purpose they fulfill.
- Unions These overlay items of different base types in the same memory location. Unions require a deeper understanding of C’s memory model and are not of much use in a programmer’s everyday life, so they are only introduced later, in subsection 12.2.
There is a fifth strategy that introduces new names for types: typedef
(section 6.4). Unlike the previous four, this does not create a new type in C’s type system, but only creates a new name for an existing type. In that way, it is similar to the definition of macros with #define
, thus the choice for the keyword for this feature.