List Of Topics

# Structures

We have studied arrays which is a data structure containing elements of same type. Now we will study structures whose individual elements are of different type, which resembles the real world application closely.

In this chapter we will see how structures are defined and how their individual members are accessed and used within a program. We will see how pointers, array and functions are related to structures. We will also study unions, which are slightly different from structures and how they are used. We will introduce the concept of bit-fields which is basically a space-saver.

What is a structure and why structures are used?

Structure is a collection of data items of different data-types. We have seen that a variable can hold a single data item. Array can hold a collection of data items but all must be of the same type. Only arrays are insufficient, because in real life if we consider any object, entities related to that object are of various types. For e.g. if we want to keep a record of all the employees in a company, an employee number of integer type and salary type float. This can be done by using one array. One integer array for employee number and a third array of floating point numbers for salary.

The following code snippet shows how a record of two employees is created using arrays.
Example(1)

`#include<stdio.h>void main(){int emp_no[4];float salary[4];int j=0;printf("enter details of employeen");for(j=0;j<=2;j++){scanf("%d %f",&emp_no[j],&salary[j]);printf("n");}for(j=0;j<=2;j++){printf("n%dt%f",emp_no[j],salary[j]);}}`

Since we have considered only 2 entities emp_no and salary we require only 2 arrays. The number of arrays will go on increasing as more and more items are added. It will then become infeasible. Structures make things simpler. Also using structures it is clear that name, emp_no and salary are all related to employee.

The general format of structure definition is

`struct tag {member 1;member 2;…………..member 3;};`

Remember the semicolon after the closing brace brackets. ‘struct’ is the keyword which is required, ‘tag’ is the name of the structure and members are the elements of the structure. Once type of structure, i.e. its composition is defined, individual variables can be declared as storage-class.struct tag var1, var2, ………varn where var1, var2….varn are structure variables of type tag. The structure composition and variable declaration can be combined

`struct tag {member 1;member 2;…………..member n;}var1, var2,…. varn;`

Let us write the previous program using structures.

```struct emp{char name [15];int emp_no;float salary;
}; ```

The name of structure is ‘emp’. It contains members, an array of characters, an integer, and a float variable. An individual member of a structure can be of our usual data-type(int, char..) or array or pointer or other structures. The names of all members within a structure should be distinct. They can be same as any other variable outside the structure. The declaration of structure as above doesn’t reserve any space but just gives the format of structure i.e. its type.

Like in an array, each member of a structure is stored beside each other

Now let us define variables which will use struct emp

`struct emp e1, e2, e3;`

e1, e2, e3 are variables of type emp. i.e. each will use format of struct emp. Also this statement assigns space in memory for each variable. Memory assigned to a structure variable is equal to storage space required for all members added. i.e. for each e1, e2, e3

Space assigned = 15 + 2 + 4 (15 for char array + 2 for integer + 4 for float)
= 21 bytes
The format of structure and variable definition combined

`Struct emp{char name [15];int emp_no;float salary;} e1, e2, e3;`

The drawback of eliminating structure name is that later on in the program we cannot define more variables of same format using name of structure. All needed variables are to be defined with format itself at the beginning.

typedef
At this point let us introduce the feature of typedef. Using typedef we can assign a new name to an existing data-type or a complex data-type. It will be said to be user defined data type. Variables can be defined using this user defined data type; once it is established.
Its general form is
Typedef type new-type;

E.g. if we say typedef int tool;
Tool is the new user defined data type and can be used in place of int. and we can use tool x, y, z; in place of int x, y, z;

It is to be noted that new data-type is not established but a new name is given to existing data-type. Typedef feature is particularly convenient for structures since its use eliminates the repeated use of struct tag while defining variables. Also the new name often suggests the purpose of the structure.
We can write

`typedef struct emp{char name [15];int emp_no;float salary;} record;`

And define variables of struct emp using
record e1, e2, e3;

Initializing Structures
We cannot initialize individual structure members at the time of stating its format.
i.e.

`struct emp{ char name [15] = “Tom”;int emp_no = 5;float salary = 11800.56;};`

Is not allowed.
Structure can be initialized as follows:

`struct emp e1 = {“John”, 5, 11800.56};struct emp e2 = {“Jack”, 6, 10000.00};`

Just as we initialize arrays by enclosing the values separated by commas in brace brackets. During initialization, the values should be given in the order in which the members are set.

Accessing members of structures
Let us see how we can use individual members of structure. A structure member can be accessed by writing structure variable. member
The dot (‘.’) is called the structure member operator which connects the structure variable name and the member.

If individual member is an array then

`variable.member[expression]`

expression is non-negative integer representing array elements.

Members of structures can be operated as individual separate entities. ‘.’ Operator falls into the highest precedence group and its associativity is left to right.

For e1 employee structure, the number of employee can be obtained as e1.emp_no. While the employees salary can be obtained as e1. salary. If we want to know the 5th character of employees name it can be done by saying e1.name[5].

Once you know how individual members of structure are accessed they can be used like any other ordinary variables. The operations performed on entire structure are: address of structure can be obtained, one structure can be assigned directly to other structure provided format of both structures are same.

Using all the explanation given till now let us modify above example using structures

/* Data related to employees employee number, salary and name of employee is stored and retrieved using a structure */

`#include<stdio.h>main ( ){typedef struct emp{char name [15];int emp_no;float salary;} record;record e1, e2;printf("nEnter employee name, employee number and salary of 2 employees :");scanf("%s %d %f ", e1. name, &e1.emp_no, &e1.salary);printf("n");scanf("%s %d %f", e2. name, &e2.emp_no, &e2.salary);printf("n");printf("n%s %d %f", e1. name, e1.emp_no, e1. salary);printf("n%s %d %f", e2. name, e2.emp_no, e2.salary);}`

Structure format is declared variable e1 and e2 are defined. Values of employee no, name and salary for both variable e1 and e2 are taken from user and printed.

 e-University Search   Related Jobs No Jobs Found.