What is a structure and why structures are used? C

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.

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.

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

each member of a structure is stored beside

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

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.

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

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.

Is not allowed.
Structure can be initialized as follows:

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


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 */

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.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics