written 6.1 years ago by |
C supports a constructed data type known as structures, a mechanism for packing data of different types. A structure is a convenient tool for handling a group of logically related data items.
For example, it can be used to represent a set of attributes, such as student _ name, roll _ number and marks. The concept of a structure is analogous to that of a ‘record’ in many other languages. More examples of such structures are:
time : seconds, minutes, hours
data : day, month, year
book : author, title, price, year
city : name, country, population
Unlike arrays, structure must be defined first for their format that may be used later to declare structure variables. Let us use an example to illustrate the process of structure definition and the creation of structure variables. Consider a book database consisting of book
name, author, number of pages, and price. We can define a structure to hold this information as follows:
struct book _bank
{
char title[20];
char author[15];
int pages;
float price;
};
The keyword struct declares a structure to hold the details of four data fields, namely title, author, pages, and price. These fields are called structure elements or members. Each member may belong to different type of data. book_bank is the name of the structure and is called the structure tag. The tag name may be used subsequently to declare variables that have the tag’s structure.
The general format of a structure definition is as follows:
struct tag _ name
{
data _ type member1;
data _ type member2;
------------
--------
----
};
In defining a structure, we may note the following syntax:
The template is terminated with a semicolon.
While the entire definition is considered as a statement, each member is declared independently for its name and type in a separate statement inside the template.
The tag name such as book _ bank can be used to declare structure variables of its type, later in the program.
Giving Values To Members
We can access and assign values to the members of a structure in a number of ways. The members themselves are not variables. They should be linked to the structure variables in order to make them meaningful members. For example, the word title has no meaning, whereas the phrase ‘title of book’ has a meaning. The link between a member and a variable is established using the member operator ’.’,which is also known as ‘dot operator’ or ‘period operator’. For example,
book1.price
is the variable representing the price of the book1 and can be treated like any other ordinary variable. Here is how we would assign values to the member of book1:
strcpy(book1.title, “COBOL”);
strcpy(book1.author, “M.K.ROY”);
book1.pages = 350;
book1. price =140;
We can also use scanf to give the values through the keyboard.
scanf(“%s\n”, book1.title); scanf(“%d\n”, &book1.pages);
are valid input statements.
Example :
Define a structure type, struct personal, that would contain person name, date of joining and salary. Using this structure, write a program to read this information for one person from the keyboard and print the same on the screen. Structure definition along with the program is shown below. The scanf and printf functions illustrate how the member operator ‘.’ is used to link the structure members to the structure variables. The variable name with a period and the member name is used like an ordinary variable.
struct personal
{
char name[20];
int day;
char month[10];
int year;
float salary;
};
main()
{
struct personal person;
printf(“Input values\n”);
scanf(“%s %d %s %d %f”,person .name, &person. day,
person.month,,&person.year,&person.salary); printf(“%s %d %s %d %.2f\n”, person .name, person. day,
person.month, person.year, person.salary);
}
Structure Initialization
Like any other data type, a structure variable can be initialized at compile time.
main()
{
struct
{
int weight; float height;
}
student ={60, 180.75};
………
………
}
This assigns the value 60 to student. weight and 180.75 to student. height. There is a one-to-one correspondence between the members and their initializing values.