The general form of a structure deceleration is shown here
struct struct-type-name{
type element_name1;
type element_name2;
..
..
} structure-variables;
Let's begin with an example that define a structure that can hold information
relating to a course grades. Grade records typically consists of several pieces of
information, such as student names, last four digits of social security number,
first exam grades, second exam grades, homework grades, final grades and so on. A
structure is a good way to manage this information. The following code fragment
declares a structure that defines student name, SSN, first_exam, second_exam,
homework and final. The keyword struct tells the compiler that a structure
declaration is beginning.
struct grades {
char name[20]; // names of 20 students
int ssn; //Social security numbers
double first_exam;// first exam
double second_exam;//second exam
float homework;//homework
double final;// final
};
Notice the declaration is terminated by a semicolon. This is because a structure
declaration is a statement. The type name of the structure is grade. In the
preceding declaration, no variable has actually been created. Only the form of the
data has been defined. To declare an actual variable, you would write something like
this:
grade varA;// grade is the type and varA is the name of variable
Remember when you define a structure, you are defining a new data type. It is not until you declare a variable of that type that one actually exists.
You can also declare one or more variables at the same time you define a structure
as shown here:
struct grades{
char name[20]; // Name of a student with 20 characters
int ssn; //Social security numbers
double first_exam;// first exam
double second_exam;//second exam
float homework;//homework
double final;// final
}varA, varB, VarC;
This fragment defines a structure type called grade and declares variables varA, varB and varC of that type.