Structure Assignments and Arrays of Structures
Structure Assignments
The information contained in one structure may be assigned to another structure of the same type using a single assignment statement. That is, you do not need to assign the value of each member separately. The following program illustrates structure assignments:
#include
int main(void)
{
struct {
int a;
int b;
} x, y;
x.a = 10;
y = x; /* assign one structure to another */
printf(”%d”, y.a);
return 0;
}
After the assignment, y.a will contain the value 10.
Arrays of Structures
Perhaps the most common usage of structures is in arrays of structures. To declare an array of structures, you must first define a structure and then declare an array variable of that type. For example, to declare a 100-element array of structures of type addr, defined earlier, write
struct addr addr_info[100];
This creates 100 sets of variables that are organized as defined in the structure addr.
To access a specific structure, index the structure name. For example, to print the ZIP code of structure 3, write
printf(”%d”, addr_info[2].zip);
Like all array variables, arrays of structures begin indexing at 0.
Popularity: 1% [?]