Node:Arrays of structures, Next:, Previous:Using structures, Up:struct

Arrays of structures

Just as arrays of basic types such as integers and floats are allowed in C, so are arrays of structures. An array of structures is declared in the usual way:

struct personal_data my_struct_array[100];

The members of the structures in the array are then accessed by statements such as the following:

The value of a member of a structure in an array can be assigned to another variable, or the value of a variable can be assigned to a member. For example, the following code assigns the number 1965 to the year_of_birth member of the fourth element of my_struct_array:

my_struct_array[3].year_of_birth = 1965;

(Like all other arrays in C, struct arrays start their numbering at zero.)

The following code assigns the value of the year_of_birth member of the fourth element of my_struct_array to the variable yob:

yob = my_struct_array[3].year_of_birth;

Finally, the following example assigns the values of all the members of the second element of my_struct_array, namely my_struct_array[1], to the third element, so my_struct_array[2] takes the overall value of my_struct_array[1].

my_struct_array[2] = my_struct_array[1];