Computer Concept & Programming In C - Unit IV - 6

Q.11 What do you understand by structures and explain purpose & usage of structures?
Related Questions -
Q. What is the purpose of using structures in C? Explain with the help of a suitable example.                                                                  (AKTU. 2009-10)
Q. What is structure?                                                  (AKTU. 2011 - 12)
Ans. Structures: -
We can group variables of different types with a data type called a structure. In C, the structure collects different data items in such a way that they can be referenced as a single unit.
There are several major differences between an array and a structure. Beside the fact that the data items in a structure can have different types each data item has its own name instead of a subscript value. In fact the data items in a structure are called field members of the structure.
Declaring Structure: -
The general form to declare a structure is 
struct struct-tag{
data-type1 variable1;
data-type2 variable2;
data-type3 variable3;
}
here struct is a keyword used in C to start a structure declaration. Struct-tag is the name for the structure. Variable1, variable2 and variable3 are the members of the structure. Their data types are specified respectively by data-type1, data-type2 and data-type3. The declarations of the members have to be enclosed within the opening and closing braces ({and}) in the structure declaration and a semicolon (;) has to be included at the end of the declaration.
The following is an example of a structure declaration:
struct automobile
{
int year;
char model [8];
int engine_power;
float weight;
}
here struct is used to start a structure declaration, automobile is the tag name of the structure. In this example, there are three types of variables, char, int, and float. The variables have their own names, such as year, model, engine-power and weight.
Note that a structure tag name, like automobile, is labeled to a structure. The compiler uses the tage name to identify the structure labeled by that tag name.
Defining structure variables: -
After declaring a structure, you can define the structure variables. For instance the following structure variables are defined with the structure data type of automobile from the previous section.
struct automobile sedan, pick-up, sport-utility;
Here, three structure variables, sedan, pick-up and sport-utility are defined by the structure of automobile. All three structure variables contain the four members of the structure data type of automobile. Also you can combine the structure declaration and definition into one statement like:
struct automobile {
int year;
char model [8];
int engine-power;
float weight;
} sedan, pick-up, sport_utility;
struct automobile {
int year;
char model [8];
int engine_power;
float weight;
sedan, pick-up, sport-utility;
here three structure variables, sedan, pick-up and sport utility are defined with the structure data type of automobile in the single statement. 
Referencing structure members with the dot operator: -
Now let us see how to reference a structure member. Given the structure data type of automobile and the structure of sedan, for instance, one can access its member, year and assign an integer to it in the following way:
Sedan.year = 1997;
Here the structure name and its members are separated by the dot (.) operator so that the compiler knows that the integer value of 1997 is assigned to the variable called year, which is a member of the structure sedan.
Like wise the following statement assigns that the start address of the character array of model, which is another member of the structure sedan, to a char pointer, Ptr:
ptr = sedan.model;
The following program gives another example to reference the members of a structure.
Referencing the member of structure and assigning of structures: -
# include <stdio.h>
# include <conio.h>
void main ( )
{
struct computer 
{
float cost;
int year;
int cpu_speed;
char cpu_type [16];
}model;
printf (“\ n The type of the CPU inside your computer?”);
gets (model.CPU_type);
printf (“\ n The speed (MHz) of the CPU?”);
scanf (“%d”, & model.CPU_speed);
printf (“\n The year your computer was made?”);
scanf (“%d”, & model.year);
printf (“\n How much you paid for computer?”);
scanf (“%f ”, & model.cost);
printf (“\n Here are what you entered:”);
printf (“\n Year: %d”, model.year);
printf (“\n Cost: $%6.2f ”; model.cost);
printf(“\n CPU type: %s”, model.CPU_type);
printf (“\n CPU speed: % d MHz”, model.CPU_speed);
}
The following is the output of the preceding program.
The type of the CPU inside your computer?
Pentium
The speed MHz of the CPU?
100
The year your computer was made?
1996
How much you pad for the computer?
3478.16
Here are what you entered:
Year: 1996
Cost: $ 3478.16
CPU_type: Pentium
CPU_speed: 100 MHz.
Initializing Structures: -
A structure can be initialized by a list of data called initializers. Commas are used to separate data items in a list of data.
# include <stdio.h>
# include <conio.h>
void main ( )  
{
struct employee
{
int id;
char name [32];
}; /* struct initialization */
struct employee info = {
1,
“B. Smith”
};
printf (“\n Here is a sample:”);
printf (“\ n Employee Name: % s”, info.name);
printf(“\ n Employee 10 #: % 04d\n\n”, info.id);
}
The output of the program is 
Here is a sample:
Employee Name: B. Smith
Employee 10 #: 0001