Aggregate Data Types

Aggregate data types are built from one or more of the basic data types. These
include the following:

Arrays (including strings)

Structures

Unions

Arrays and Strings

An “array” is a collection of data elements of a single type. An array can contain any data type. You can access an element of an array by using the array name and a numeric subscript.

A “string” is an array of characters that terminates with the null character (\0).
Arrays that contain strings must allow space for the final null character.

Typical arrays and strings are shown below:

int id_number[10]; /* One-dimensional;

10 elements; integer */

char name[30]; /* String */

float matrix[5][3]; /* Two-dimensional array,

5 rows, 3 columns */

char baby[30] = "Peter Roddy"; /* String initialization */

Structures

A “structure” is a collection of data items of different types. Once you have defined a structure type, you can declare a structure variable using that type.

The following example illustrates a simple structure:

struct date

{

int month;

int day;

int year;

}

struct date today;

The example defines a structure type named date and declares a structure variable today to be of type date.

Use the structure-member operator ( . ) to access the “elements” (members) of a structure. The name

today.month

refers to the month member of the today structure in the example.

Unions

A “union” is a set of data items of different types sharing the same storage space in memory. One use of unions is accessing the computer's DOS registers. For instance, QuickC defines the union REGS as the following:

union REGS

{

struct WORDREGS x;

struct BYTEREGS h;

};