A member of a structure may be either a simple or compound type. A simple
member is one that is of any of the built-in data types, such as integer or character.
You have already seen one type of compound element: the character arrays used in
addr. Other compound data types include one-dimensional and multidimensional
arrays of the other data types and structures.
A member of a structure that is an array is treated as you might expect from
the earlier examples. For example, consider this structure:
struct x {
int a[10][10]; /* 10 x 10 array of ints */
float b;
} y;
To reference integer 3,7 in a of structure y, write
y.a[3][7]
When a structure is a member of another structure, it is called a nested structure.
For example, the structure address is nested inside emp in this example:
struct emp {
struct addr address; /* nested structure */
float wage;
} worker;
Here, structure emp has been defined as having two members. The first is a structure
of type addr, which contains an employee's address. The other is wage, which holds
the employee's wage. The following code fragment assigns 93456 to the zip element
of address.
worker.address.zip = 93456;
As you can see, the members of each structure are referenced from outermost to
innermost. Standard C specifies that structures may be nested to at least 15 levels.
Standard C++ suggests that at least 256 levels of nesting be allowed.
No comments:
Post a Comment