0% found this document useful (0 votes)
10 views24 pages

8 Structures

Uploaded by

soumendhar393
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

8 Structures

Uploaded by

soumendhar393
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

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};

Note: the compile time initialization of structure


variable must have the following elements:
1. The keyword struct
2. The structure tag name
3. The name of the variable to be declared.
4. The assignment operator =
5. A set of values for the members of the structure
variable, separated by commas and enclosed in
braces.
6. A terminating semicolon
Structure Initialization at Run time

An structure can be explicitly initialized at run time. For example,


consider the following segment of a C program.
We use scanf to give the values through the keyboard.
scanf(“%d”, &student1.weig ht );
scanf(“%f”, &student1.height);
scanf(“%d”, & student2.weig ht );
scanf(“%f”, & student2.height);
Define a structure type, struct personal that would contain name
of a person, date of joining, and salary. Using this structure, write
a program to read this information for one person from keyboard
and print the same on the screen.
#include<stdio.h>
struct personal
{
char name [20];
int day;
char month[10];
int year;
float salary;
};
main ()
{
struct personal person;
printf ("Input Values (Separated by space)\n");
printf ("Name of Person, Date of Joining (Day Month Year ), Salary of the Person\n");
scanf ("%s %d %s %d %f", person.name, &person.day, person.month, &person.year,
&person.salary);
printf ("\nName of Person : %s \nDate of Joining : %d %s %d \nSalary of the Person :
%f\n", person.name, person.day, person.month, person.year, person.salary); }
Output
1. Arrays of Structures
struct marks student[3]
It defines array called student, that consists of 3 elements.
Consider the following declaration:
struct marks
{
int subject1;
int subject2;
int subject3;
};
main ()
{
struct marks student[3] = {{45,68,81}, {75,53,69},{57,36,71}};
}
Struct marks student[3] = {{45,68,81}, {75,53,69},{57,36,71}};

This declares the student as an array of three elements


student[0], student[1], student[2] and initializes their
member as follows;
studnent[0].subject1 = 45;
studnent[0].subject2 = 68;
studnent[0].subject3 = 81;
studnent[1].subject1 = 75;
studnent[1].subject2 = 53;
studnent[1].subject3 = 69;
studnent[2].subject1 = 57;
studnent[2].subject2 = 36;
studnent[2].subject3 = 71;
#include<stdio.h>
struct marks
{
int subject1;
int subject2;
int subject3;
};
main ()
{
struct marks student[3] = {{45,68,81}, {75,53,69},{57,36,71}};
printf("student[0].subject1= %d\n", student[0].subject1);
printf("student[0].subject2= %d\n", student[0].subject2);
printf("student[0].subject3= %d\n", student[0].subject3);
printf("student[1].subject1= %d\n", student[1].subject1);
printf("student[1].subject2= %d\n", student[1].subject2);
printf("student[1].subject3= %d\n", student[1].subject3);
printf("student[2].subject1= %d\n", student[2].subject1 );
printf("student[2].subject2= %d\n", student[2].subject2);
printf("student[2].subject3= %d\n", student[2].subject3);
}
Output
Rewrite the program using for loop
#include<stdio.h>
struct marks
{
int subject1;
int subject2;
int subject3;
};
main ()
{
struct marks student[3] = {{45,68,81}, {75,53,69},{57,36,71}};
int i;
for(i=0; i<3; i++)
printf("student[%d].subject1= %d\n", i, student[i].subject1);
printf("student[%d].subject2= %d\n", i, student[i].subject2);
printf("student[%d].subject3= %d\n", i, student[i].subject3);
}
2. Arrays within Structures
C permits the use of arrays as structure members. We can use
arrays of character, integer, float type data inside structure.
Consider the following declaration:
struct marks
{
int subject[3];
} student[3];
Here, the member contains three elements, subject[0],
subject[1], and subject[2]. These elements can be accessed
using appropriate subscripts. For example:
student[1].subject[2]
Would refer to the marks obtained in the third subject by
second student.
#include<stdio.h>
struct marks
{
int subject[3];
};
main ()
{
struct marks student[3] = {{45,68,81}, {75,53,69},{57,36,71}};
int i, j;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("student[%d].subject[%d]= %d\n", i, j, student[i].subject[j]);
}
}
}
Output
3. Structures Within Structures
• Structure can be nested within other structures in C programming.
struct salary
{ The members contained in the inner structure
char name; namely dearness, house_rent, and city can be
char department; referred to as:
struct detail employee.allowance.dearness
{ employee.allowance.house_rent
int dearness; employee.allowance.city
int house_rent;
Also,
int city; The members contained in the outer structure
} namely name, department can be referred to
allowance; as:
} employee;
employee.name
employee.department
Write a program to add two distance
(in inch-feet) system using structures
#include<stdio.h> printf ("Enter information for Second
struct distance distance\n");
{ printf("Enter feet: \t");
scanf("%d", &d2.feet);
int feet;
printf("Enter inch: \t");
float inch; scanf("%f", &d2.inch);
} d1, d2, sum; sum.feet = d1.feet + d2.feet;
main() sum.inch = d1.inch + d2.inch;
{ if (sum.inch>12.0)
printf ("Enter information for Ist {
distance\n");
sum.inch = sum.inch -12;
printf("Enter feet: \t");
++sum.feet;
scanf("%d", &d1.feet);
}
printf("Enter inch: \t");
printf("sum of distances = %d'- %0.1f",
scanf("%f", &d1.inch); sum.feet, sum.inch);
}
Output
?

You might also like