09 Structures
09 Structures
1
INTRODUCTION
◼ Arrays can be used to represent a group of data
items that belong to the same type such as int or
float.
◼ Problem: we cannot use an array if we want to
represent a collection of data items of different type
using a single name.
◼ Solution: C supports a constructed data type known
as structures→a mechanism for packing data of
different types.
2
What is a Structure?
◼ Used for handling a group of logically
related data items
Examples:
◼ Student name, roll number, and marks
4
Contd.
◼ The individual members can be ordinary
variables, pointers, arrays, or other structures
(any data type)
The member names within a particular
structure must be distinct from one another
A member name can be the same as the
name of a variable defined outside of the
structure
◼ Once a structure has been defined, the
individual structure-type variables can be
declared as:
struct tag var_1, var_2, …, var_n;
5
Example
◼ A structure definition
struct student {
char name[30];
int roll_number;
int total_marks;
char dob[10];
};
A new data-type
6
A Compact Form
◼ It is possible to combine the declaration of the
structure with that of the structure variables:
struct tag {
member 1;
member 2;
:
member m;
} var_1, var_2,…, var_n;
9
Structure Initialization
◼ Another method to initialize structure variable outside
function:
◼ struct st-record
◼ {
◼ int weight;
◼ float height:
◼ }student1={ 60,180.75};
◼ void main( )
◼ {
◼ struct st-record student2={53,170.60};
◼ }
10
Example: Complex number addition
void main()
{
struct complex
{
float real;
float cmplex;
} a, b, c;
12
◼ #include <stdio.h>
Example2
◼ #include <string.h>
◼ struct book{
◼ char title[10];
◼ char author[20];
◼ double price;
◼ int pages;
◼ } book1;
◼ int main(){
◼ strcpy(book1.title, "Learn C");
◼ strcpy(book1.author, "Dennis Ritchie");
◼ book1.price = 675.50;
◼ book1.pages = 325;
◼ printf("Title: %s \n", book1.title);
◼ printf("Author: %s \n", book1.author);
◼ printf("Price: %lf \n", book1.price);
◼ printf("Pages: %d \n", book1.pages);
◼ return 0;
◼ }
13
Operations on Structure
Variables
◼ Unlike arrays, a structure variable can be directly
assigned to another structure variable of the
same type
a1 = a2;
◼ All the individual members get assigned
14
Arrays of Structures
◼ Let us consider we have a structure as:
◼ struct student
◼ {
◼ char name[20];
◼ int rollno;
◼ float marks;
◼ };
◼ If we want to keep record of 100 students, we have to make 100
structure variables like st1,st2,st3,………….st100.
◼ In this situation we can use array of structure to store the records of
100 students which is easier and efficient to handle.
15
Arrays of Structures
◼ Two ways to declare an array of structure:
◼ struct student struct student
◼ { {
◼ char name[20]; char name[20];
◼ int rollno; int rollno;
◼ float marks; float marks;
◼ }st[100]; };
◼ struct student st[100];
◼
16
Example to illustrate Arrays of Structure
struct student{
char name[10];
int rollno;
float marks;
};
int main(){
struct student b[3];
strcpy(b[0].name, ”John");
b[0].rollno = 34;
b[0].marks= 32.5;
strcpy(b[1].name, ”Tommy");
b[1].rollno = 17;
b[1].marks = 22.5;
strcpy(b[2].name, ”Richie");
b[2].rollno = 25;
b[2].marks = 32.5;
printf("\nList of Students:\n");
for (int i = 0; i < 3; i++){
printf(”Name: %s \t Rollno: %d \t Marks: %f\n", b[i].name, b[i].rollno, b[i].marks);
}
return 0;
}
17
Arrays within Structures
◼ A structure member can be an array
struct student
{
char name[30];
int roll_number;
int marks[5];
char dob[10];
} a1, a2, a3;
◼ The array element within the structure can
be accessed as:
a1.marks[2], a1.dob[3],…
18