Structures
The secondary data type, which associates with a set of similar data types, is array data type. We have already discussed array of ints, array of floats or array of char’s etc. These set represent the characteristics of samedata type . But we are living in such an environment that in our daily life we need to associate a single variable to different data types. For example let an employee of an institution is referred as its:
First name
Second name
Age
Salary
Department etc.
C provides a data type structure to handle such type of environment. A structure is basically a collection of data fields in which the fields may or may not belong to same category. And the fields of the structure are called as members of the structure.Now we will see how to define structure data type. C provides a keyword ‘ struct ’ to declare a structure. The syntax of a structure declaration is as:
struct{
datatype_1 list of datatype_1 variables;
datatype_2 list of datatype_2 variables;
datatype_3 list of datatype_3 variables;
datatype_m-1 list of datatype_m-1 variables;
datatype_m list of datatype_m variables;
};
For example if we want collect the information of all the employees have as of an institution then let it has following information:
First name
Second name
Age
Salary
Department
City
Pincode
Then we will use the following structure declaration:
struct employee{
char f_name [20];
char s_name [20];
int age;
float salary;
char dept [25];
char city [15];
long int pincode; };
Here employee is not a variable, rather than it is a structure declaration, which is used to create structures, or you can say employee is new user structure defined data type. Since a structure declaration does not reserve any space in memory. Therefore employee is a structure declaration, so it will not reserve any space in memory.If we want to create one or two variables of data type employee then we will use the following statement:
struct employee emp1, emp2;
Here emp1 and emp2 are two structure variables of data type employee. And now the compiler reserves space in memory for each variable. The compiler reserves 80 bytes for each structure variables of type employee as:
20 bytes are reserved for f_name
20 bytes are reserved for l_name
02 bytes are reserved for age
04 bytes are reserved for salary
15 bytes are reserved for dept
15 bytes are reserved for city
04 bytes are reserved for pincode
And these bytes are reserved contiguously in memory as
Addresses |
Contents |
68201 68202 | 68220 |
f_name |
68221 68222 | 68240 |
l_name |
68241 68442 |
age |
68443 | 68446 |
salary |
68447 | 68461 |
dept |
68462 | 68476 |
city |
68477 | 68480 |
pincode |
In this figure (a), it is assumed that the starting address as 68201.
The complete structure declaration and structure variables are as:
struct employee{
char f_name [20];
char s_name [20];
int age;
float salary;
char dept [25];
char city [15];
long int pincode;
};
struct employee emp1, emp2;
The another way of writing this is:
struct employee{
char f_name [20] ;
char s_name [20];
int age;
float salary;
char dept [25];
char city [15];
long int pincode;
} emp1, emp2;
Till now we know how to define a structure declaration and how to create structure variables. Now we will see - how to access individual member of the structure variables. C provides a dot (.) Operator to access the individual member of the structure variables. The syntax of accessing the member of a structure variable is as:
For example, if we want to refer the first name of the structure variable emp1 then we will use:
emp1.f_name;
Similarly if we want to refer the department of the structure variable emp2 then we will use:
emp2.dept;
Now let us write a simple program in which we store the values into three structure variables and output these values:
/* Program – 1 : Simple structure */
#includemain ()
{
struct employee
{
char name [20];
int age;
float salary;*
long int pincode;
};
struct employee e1, e2, e3;
printf (“\n Enter the name, age, salary and pin code of first employee =”);
scanf (“%s %d %f %ld”, e1.name, &e1.age, &e1.salary, &e1.pincode) ;
printf (“\n Enter the name, age, salary and pin code of second employee =”);
scanf (“%s %d %f %ld”, e2.name, &e2.age, &e2.salary, &e2.pincode) ;
printf (“\n Enter the name, age, salary and pin code of third employee =”);
scanf (“%s %d %f %ld”, e3.name, &e3.age, &e3.salary, &e3.pincode);
printf (“\n You have entered following values = ”);
printf (“%s %d %f %ld”, e1.name, e1.age, e1.salary, e1.pincode);
printf (“%s %d %f %ld”, e2.name, e2.age, e2.salary, e2.pincode);
printf (“%s %d %f %ld”, e3.name, e3.age, e3.salary, e3.pincode);
}
The output of this program is
Enter the name, age, salary and pin code of first employee =
Ambit 28 12000.00 126001
Enter the name, age, salary and pin code of second employee =
Ramesh 32 10075.75 145600
Enter the name, age, salary and pin code of third employee =
Sumit 26 9000.50 127006
You have entered following values
Amit 28 12000.00 126001
Ramesh 32 10075.75 145600
Summit 26 9000.50 127006
Structure Variable Initialization
Like ints, floats, chars and arrays, we can also initialize structure variables as:
struct employee{
char name [20] ;
int age ;
float salary ;
long int pincode;
};
struct employee emp1 = {
“Amit” , 28 , 12000.00 , 126001
};
struct employee emp2 = { “Ramesh”, 32 10075.75, 145600 };
struct employee emp3 = { “Sumit”, 26 , 9000.50 , 127006 };
Array of Structures
Till now we have defined structure declaration and structure variables. But some we need to store a set of 100 employees records or a set of 50 student records, then we need to maintain a number of structure variables.
There are ways to handle this problem:
- Either uses 100 different structure variables such as emp1, emp2, and emp3.... emp100
- or use an array of structures that store all hundred structures into one umbrella
To declare an array of structure, we must define a struct data type and then declare as array of structure. Let us declare an array of 50 structure variables that holds the records of 50 students.
struct student_rec{
char name[20];
int age;
long int rollno;
char section;
};
struct student_rec student[50];
C compiler reserves memory space for 50 structures after the execution of this statement declaration and the same rule applies for array of structure as for array of primary data types such as ints, floats or chars i.e. the first student record is referred as:
student [0].namestudent [0].age
student [0].rollno
student [0].section
and the 50th record is referred as :
student [49].name
student [49].age
student [49].rollno
student [49].section
And in memory all the structure variables are stored in contiguous memory locations because array elements are stored continuously in memory and here array element is a structure and members of structure are always stored contiguously. It means that first one student [0]’s name, age, rollno and section would be stored, then student [1]’s name, age, rollno and section, student [2]’s name, age, rollno and so on.
Let us look at a complete program that uses array of structures:
/* Program – 2 : Array of structures */
#includemain ()
{
struct student_rec
{
char name[20];
int age;
long int rollno;
char section;
};
struct student_rec s[50] ;
int i ;
for (i=0; i<= 49 ; i++ )
{
printf (“\n Enter the name, age, roll number and section of %d student”, i+1);
scanf (“%s %d %ld %c”, s[i].name, &s[i].age, &s[i].rollno , &s[i].section ) ;
}
for (i=0; i<= 49 ; i++ )
{
printf (“\nThe name, age, roll number and section of %d student is \n”,i+1);
printf (“%s %d %ld %c”, s[i].name, s[i].age, s[i].rollno , s[i].section);
} }
Nested Structures
C also provides the facility of nested structure i.e. declaring one structure within another structure. Following program illustrates this concept of nested structure:
/* Program – 3 : Nested structures */
#includemain ()
{
struct address
{
char street[20] ;
char city[20] ;
char state[15] ;
long int pincode;
};
struct stud
{
char f_name[20];
char l_name;
int age;
struct address addr;
};
struct stud s1 = {“Amit”, “Kr.”, 32, “Raj Marg”, “Bomby”, “MH” , 400001 } ;
struct stud s2 ;
printf (“\nEnter the first name , last name , age , street , city , state and pincode”);
scanf (“%s %s %d”, s2.f_name, s2_lname, s2.age) ;
scanf (“%s %s %s %ld”, s2.addr.street, s2.addr.city, s2.state, s2.addr.pincode);
printf (“\nFirst student record = ”);
printf (“\nFirst name = %s”, s1.f_name);
printf (“\nLast name = %s”, s1.l_name);
printf (“\nAge = %d”, s1.age);
printf (“\nStreet = %s”, s1.addr.street);
printf (“\nCity = %s”, s1.addr.city);
printf (“\State = %s”, s1.addr.state);
printf (“\nPincode = %s”, s1.addr.pincode);
printf (“\nSecond student record = ”);
printf (“\nFirst name = %s”, s2.f_name);
printf (“\nLast name = %s”, s2.l_name);
printf (“\nAge = %d”, s2.age);
printf (“\nStreet = %s”, s2.addr.street);
printf (“\nCity = %s”, s2.addr.city);
printf (“\State = %s”, s2.addr.state);
printf (“\nPincode = %s”, s2.addr.pincode);
}
The output of this program is as:
Enter the first name, last name, age, street, city, state and pincode
First name = Maggy
Last name = Dsouza
Age = 45
Street = Mal Road
City = Salcette
State = Goa
Pincode = 444000
First student record =
First name = Amit
Last name = Kumar
Age = 32
Street = Raj Marg
City = Bombay
State = MH
Pincode = 400001
Second student record =
First name = Maggy
Last name = Dsouza
Age = 45
Street = Mal Road
City = Salcette
State = Goa
Pincode = 444000
The main thing in this program is to note – how do we access members of structure, which is a part of another structure. In this program
struct address is used in struct stud.
Thus when we access the members of structure address through structure variables of type student_rec, we will use dot (.) operator twice as:
s1.addr.streets1.addr.city
s1.addr.state
s1.addr.pincode