This chapter is going to introduce structures in C. Structures is one of the important concepts of C programming. Structure is known an user defined data type. You must be thinking when we have so many data types then why we need structures? You would remember that array is an collection of alike data items. Practically, we need to deal with miscellaneous collection of data items and that is possible because of Structure.
16.1Introduction to Structures
Logically related data items can be stored under one name. Data items can be alike or different. Variables can be accessed and each variable corresponds to an item in the structure. Each item is known as member or field of structure which do have a particular data type. Name of the structure is also known as tag name. Syntax of structure is as follows:
struct name_of_tag
{
data_type_1 member_1;
data_type_2 member_2;
…. ….
};
Figure Format of structure
Explanation: Here,
struct is the keyword and it is an indicator for compiler that structure is being defined.
name_of_tag is usually an identifier which is the name of structure.
member_1, member_2 are called members or fields of structures.
data_type_1, data_type_2 are representing data types of member_1, member_2 respectively
Let us consider an example of defining structure,
struct employee
{
char name[20];
int employee_id;
float salary;
};
Explanation: Here,
struct is the keyword
employee is the name of structure which is an identifier
name, emplyee_id, salary are the members of structure. They are not variables hence they themselves do not consume space in computer memory.
Since we know the way of defining structure so next step is to declare structure. Like other data types, we declare structure variables. Here, struct keyword is followed by the name of identifier which is the tag name. This is followed by variables list which are separated by comma and it is terminated by semi colon. Tag name or structure definition is not associated with memory but as soon as variables are associated with structure definition, compiler allocates memory. Variables are to be declared as shown below:
below:
struct name_of_tag variable_1, variable_2;
Explanation: Here,
struct is the keyword
name_of_tag is the name of structure
variable_1, variable_2 are the structure variables.
Let us consider the above sample structure definition and declare variables according to that definition.
struct employee it, operations, training;
Explanation: Here,
struct is the keyword
employee is the tag name
struct employee when combined togerthe represents data type. This is an derived data type which is derived from basic data types
it, operations, trainingi are variables of type struct employee and memory is going to be associated with it, operations and training respectively.
Member of a structure can be accessed and they are treated as separate individuals. For accessing a member we specify the name of variable followed by period (.) which is again followed by name of the member. Syntax is as follows:
variable_1.member_1;
variable_1.member_2;
Explanation: Here,
variable_1 is representing the structure variable
member_1, member_2 are the respective members of the structure
Let us consider the above sample structure and find out the way to access it.
struct employee
{
char name[20];
int employee_id;
float salary;
};
struct employee it = {“Debasif”, 546502, 30000};
For accessing the members we will have to use the following specifications. We can access Debasif by specifying it.name. Similarly, we can access 546502 by specifying it.employee_id and 30000 can be accessed by specifying it.salary respectively.
The size of structure is the sum of sizes of each member of structure. For example,
struct employee
{
char name[20];
int employee_id;
double salary;
} it;
The size of each member of structure is as follows:
it.name is an array of characters which has 20 bytes.
it.employee_id is of type integer which is of 4 bytes
it.salary is of type double which is of 8 bytes
So the structure is of size 20+4+8=32 bytes
Now, there are chances to have size of structure not equal to sum of individual members and the reason is slack bytes. Usually, computers allocate memory for members sequentially on the basis of their size respectively. Sometimes members are allocated at some special boundaries which are known as word boundaries where additional bytes are padded at end of each member whose size is smaller as compared to largest data type so that address of each member begins at word boundary. These bytes do not contain any information and waste memory. These additional bytes which are added to maintain boundary are called slack bytes. Data present in word boundary can be accessed quickly hence slack bytes themselves are not useful but their presence can increase the rate of access.
Let us consider an example to illustrate the working principle of structure.
/* Program to illustrate structure */
# include <stdio.h> # include <conio.h> # include <stdlib.h> # include <string.h> void main() { struct employee { char name[20]; int employee_id; float salary; }; struct employee emp; emp.employee_id=546502; emp.salary=30000; strcpy(emp.name, "Debasif"); printf("Employee information\n"); printf("%d %s %7.2f\n", emp.employee_id, emp.name, emp.salary); getch(); }
Snapshot of program looks in editor as follows:
Fig - C Program illustrate structure
After compiling programs looks similar as in the following snapshot:
Figure - Output of C program illustrate structure
With this, we conclude this chapter. Next chapter will introduce unions and enumerated data types. Thank you.