- C language also provides the flexibility to create new types, known as user-defined types.
- User-defined types can be created by using structures, unions and enumerations.
- Unlike arrays, the data of different types can be grouped together and stored by making use of structures.
- A structure is a collection of variables under a single name and provides a convenient way of grouping several pieces of related information together.
- The structure definition defines a new type, known as structure type.
- The structure declaration-list in a structure definition consists of declarations of one or more variables, possibly of different types.
- A structure declaration-list cannot contain a member of void type or incomplete type or function type.
- A structure definition cannot contain an instance of itself.
- A structure definition may contain a pointer to an instance of itself. Such a structure is known as self-referential structure.
- Structure definition does not reserve any space in memory.
- It is not possible to initialize the structure members during the structure definition.
- The structure members cannot be initialized during the structure definition, but the members of a structure object can be initialized by providing an initialization list.
- An unnamed structure type is also known as an anonymous structure type.
- The member of a structure object can be accessed by using: direct member access operator or indirect member access operator.
- A structure object can be assigned to a structure variable of the same type.
- An assignment operator when applied on structure variables performs member-by-member copy.
- The members of a structure object can be byte aligned or machine-word boundary aligned.
- If the members of a structure object are machine-word boundary aligned, the padding bytes can appear in between two structure members or after the last structure member.
- The sizeof operator when applied on a structure object includes the space taken by internal and trailing padding.
- The use of the equality operator on operands of a structure type is not allowed.
- An operation that is applicable on an object of a particular type can be applied on a structure member of that type.
- Like a pointer to any other type, it is possible to create a pointer to a structure type as well.
- It is possible to define a structure type within the declaration-list of another structure-type definition.
- Unions are similar to structures except that memory is shared among all the members.
- The amount of memory allocated to a union object is the amount necessary to contain its largest member.
- Only the first member of a union object can be initialized.
- Unions are extensively used in interrupt programming.
- Enumerations provide another way to create a user-defined type. An enumeration type is designed for variables that can have a limited set of values.
- In a structure or a union declaration-list, it is possible to specify for a member, the number of bits that it will take in the memory. Such a member is called a bit-field.
- Bit-fields help in packing several objects into a single unit.