Previous: 12.1.1 Declaring and Accessing Structure Data
Up: 12.1 Structures
Next: 12.1.3 Structures and Functions
Previous Page: 12.1.1 Declaring and Accessing Structure Data
Next Page: 12.1.3 Structures and Functions
As we said above, declaring a structure variable requires two things - describing the template for the structure, and declaring variables of that structure type. It is also possible to perform these two steps in separate statements in a program. That is declare just a structure template with a tag without any variables declared; and later, declare variables of the structure type identified by the tag. For example, this declaration:
struct stdtype {
char name[26];
int id_number;
};
specifies a template with a tag for a structure type, stdtype.
(Observe the semicolon after the declaration for the declaration).
Such a
declaration does NOT allocate memory, since no variables are declared;
it merely defines a template for variables declared later. Within
the scope of the tag declaration, we can then declare stdtype structure
variables like:
struct stdtype x, y, z;This declaration allocates memory for three variables, x, y and z, of type structure stdtype; i.e. fitting the template defined earlier. Some additional examples of structure tag and variable declarations are:
/* named structure template, no variables declared */
struct date {
int month;
int day;
int year;
};
/* named structure template and a variable declared */
struct stu_rec {
char name[30];
char class_id[3];
int test[3];
int project;
int grade;
} student;
struct stu_rec ee_stu, me_stu;
struct date today, birth_day;
The main advantage of splitting the template definition from
variable declaration is that the template need be defined
only once and may then be used to declare variables in many places.
We will see the utility of this below when we pass structures
to function.
In general, then, a structure declaration has the following syntax:
struct inventory {
int item_no;
float cost;
float price;
struct date buy_date;
};
struct car_type{
struct inventory part;
struct date ship_date;
int shipment;
} car;
Here, the ship_date field of the car_type structure
is itself a date structure (from above) and the part field is
an inventory structure, with item_no,
cost, etc. fields, including yet another date structure within.
The members for nested structures may be accessed with dot operators applied
successively from left to right (the grouping for the dot operator is from left
to right). Thus:
car.ship_date.month = 5; /* Lvalue is (car.ship_date).month */
car.part.buy_date.month = 12;
these assignments refer to the month field of the ship_date
field of the variable car and the month field
of the buy_date field of the part field of the variable, car,
respectively.
Both the variables of structure type and the structure tags are frequently referred to as structures. Thus, we may refer to date as a structure, and we may say that the variable, today is a structure. It is usually clear from the context whether a structure tag or a variable of structure type is meant. However, for the most part, we will use the term structure for the templates themselves, i.e. tags; and, we will specify variables to be of structure type. Thus, date is a structure; and today is a structure variable or a variable of structure type, i.e. of type, struct date.
As with other data types, structures can be initialized in declarations by specifying constant values for the structure members within braces. The initializers for structure members are separated by commas as for an array. For example, a struct inventory item can be declared:
struct inventory part = { 123, 10.00, 13.50 };
which initializes member, part_no to 123, member cost to 10.00,
and member price
to 13.50.
As another example,
a label item can be declared as:
struct name {
char f_name[10];
char m_inits[3];
char l_name[20];
};
struct address {
char street[30];
char city[15];
char state[15];
int zip};
};
struct label {
struct name name;
struct address address;
};
struct label person = { {"Jones", "John", "Paul"},
{"23 Dole Street", "Honolulu", "Hawaii", 96822} };
The structure, label has two members,
each of which is a structure. The first member,
name, has three members, and the second member, address,
has four members.
Initialization for each member structure is nested appropriately.