8 Structures
8 Structures
Introduction
• Arrays can be used to represent a group of data items that
belong to the same type, such as int or float. However, it is
impossible to represent a collection of data items of
different types using a single name. 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. Structures help to organize complex data in a more
meaningful way.
• For example:
• Time: seconds, minutes, hours
• Date: day, month, year
• Book: author, title, price, year
• Customer: name, telephone, city, category
Defining a Structure
The general format of structure definition as
follows:
struct structure_name
{
data_type member1;
data_type member2;
-------------------------
data_type membern;
};
For example
The general format of structure definition as follows:
struct book_bank
{
char title[20]; array of 20 characters
char author[15]; array of 15 characters
int pages;
float price;
};
The keyword struct declares a structure to hold the details of four
data fields. 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 structure tag.
Declaring Structure Variables
A structure variable declaration is similar to the declaration of variables of any other data
types. It includes the following elements.
1. The keyword struct
2. the structure tag name
3. list of variable names separated by commas.
4. A terminating semicolon
struct book_bank, book1, book2, book3;
Declares book1, book2, and book3 as variables of type struct book_bank. Each one of
these variables has four members as specified by template. The complete declaration is
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
};
Inside main fucntion:
struct book_bank, book1, book2, book3;
The complier reserves memory space for the strcuture variables.
Declaring Structure Variables
It is also allowed to combine both structure
definition and variables declaration in one
statement.
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
} book1, book2, book3;
Accessing Structure Members
There are a number of ways to access and assign
values to the members of a structure. The members
themselves are not variables. They should be linked
to the structure variables in order to make them
meaningful members. 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 book1 and can
be treated like any other ordinary variable.
Structure Initialization at compile time
• A structure variable can be initialized at compile time.
struct st_record
{
int weight;
float height;
};
main()
{
struct st_record student1 = {60, 180.75};
struct st_record student2= {53, 170.50};
}
This assign the value 60 to student1.weig ht and 180.75 to
student1.height.
Similary, 53 to student2.weight and 170.50 to student2.height.
struct st_record student2= {53, 170.50};