A structure is a user defined data type. You have the ability to define a new type of data considerably more complex than the types we have been using. A structure is a combination of several different previously defined data types, including other structures we have defined.
An easy to understand definition is, a structure is a grouping of related data in a way convenient to the programmer or user of the program. The best way to understand a structure is to look at an example, so if you will load and display struct1.c, we will do just that.
main( )
{
struct {
char initial; /* last name initial */
int age; /* childs age */
int grade; /* childs grade in school */
} boy,girl;
boy.initial = ’R’;
boy.age = 15;
boy.grade = 75;
girl.age = boy.age - 1; /* she is one year younger */
girl.grade = 82;
girl.initial = ’H’;
printf("%c is %d years old and got a grade of %d\n",
girl.initial,girl.age,girl.grade);
printf("%c is %d years old and got a grade of %d\n",
boy.initial, boy.age, boy.grade);
}
The program begins with a structure definition. The key word "struct" is followed by some simple variables between the braces, which are the components of the structure. After the closing brace, you will find two variables listed, namely "boy", and "girl". According to the definition of a structure, "boy" is now a variable composed of three elements, "initial", "age", and "grade". Each of the three fields are associated with "boy", and each can store a variable of its respective type. The variable "girl" is also a variable containing three fields with the same names as those of "boy" but are actually different variables. We have therefore defined 6 simple variables.
An easy to understand definition is, a structure is a grouping of related data in a way convenient to the programmer or user of the program. The best way to understand a structure is to look at an example, so if you will load and display struct1.c, we will do just that.
main( )
{
struct {
char initial; /* last name initial */
int age; /* childs age */
int grade; /* childs grade in school */
} boy,girl;
boy.initial = ’R’;
boy.age = 15;
boy.grade = 75;
girl.age = boy.age - 1; /* she is one year younger */
girl.grade = 82;
girl.initial = ’H’;
printf("%c is %d years old and got a grade of %d\n",
girl.initial,girl.age,girl.grade);
printf("%c is %d years old and got a grade of %d\n",
boy.initial, boy.age, boy.grade);
}
The program begins with a structure definition. The key word "struct" is followed by some simple variables between the braces, which are the components of the structure. After the closing brace, you will find two variables listed, namely "boy", and "girl". According to the definition of a structure, "boy" is now a variable composed of three elements, "initial", "age", and "grade". Each of the three fields are associated with "boy", and each can store a variable of its respective type. The variable "girl" is also a variable containing three fields with the same names as those of "boy" but are actually different variables. We have therefore defined 6 simple variables.