C Programming/Composite data types

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: Pointers and arrays C Programming Next: Memory management

In the chapter C types we looked at some basic data types. However composite data types allow us greater flexibility in managing data in our C program.

Data structures[edit]

A data structure ("struct") contains multiple pieces of data. Each piece of data (called a "member") can be accessed by the name of the variable, followed by a '.', then the name of the member. (Another way to access a member is using the member operator '->'). The member variables of a struct can be of any data type and can even be an array or a pointer.


A data structure contains multiple pieces of data. One defines a data structure using the struct keyword. For example,

struct mystruct {
    int int_member;
    double double_member;
    char string_member[25];
} variable;

variable is an instance of mystruct. You can omit it from the end of the struct declaration and declare it later using:

struct mystruct variable;

It is often common practice to make a type synonym so we don't have to type "struct mystruct" all the time. C allows us the possibility to do so using a typedef statement, which aliases a type:

typedef struct {
} Mystruct;

The struct itself has no name (by the absence of a name on the first line), but it is aliased as Mystruct. Then you can use

Mystruct structure;

Note that it is commonplace, and good style to capitalize the first letter of a type synonym. However in the actual definition we need to give the struct a tag so we can refer to it: we may have a recursive data structure of some kind. For trees or chained lists, we need a pointer to the same data type in the struct. During compilation, the type synonym is not known to the compiler and there will be an error. To avoid this, it is necessary to let the compiler know the name right from the start (Note that the struct keyword is used only inside the structure! After the declaration, the compiler knows that the type synonym refers to a struct):

typedef struct Mystruct {
    struct Mystruct *pMystruct
} Mystruct;


The definition of a union is similar to that of a struct. The difference between the two is that in a struct, the members occupy different areas of memory, but in a union, the members occupy the same area of memory. Thus, in the following type, for example:

union {
    int i;
    double d;
} u;

The programmer can access either u.i or u.d, but not both at the same time. Since u.i and u.d occupy the same area of memory, modifying one modifies the value of the other, sometimes in unpredictable ways.

The size of a union is the size of its largest member.

Type modifiers[edit]

For "register", "volatile", "auto" and "extern", see C Programming/Variables#Other_Modifiers.

Previous: Pointers and arrays C Programming Next: Memory management