0% found this document useful (0 votes)
3 views

09 Structures

The document explains the concept of structures in C, which are used to group related data items of different types. It covers how to define structures, access their members, initialize them, and use arrays of structures for efficient data handling. Examples are provided to illustrate the usage of structures in various scenarios.
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)
3 views

09 Structures

The document explains the concept of structures in C, which are used to group related data items of different types. It covers how to define structures, access their members, initialize them, and use arrays of structures for efficient data handling. Examples are provided to illustrate the usage of structures in various scenarios.
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/ 18

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

◼ Real part and complex part of a complex number

◼ Helps in organizing complex data in a


more meaningful way
◼ The individual structure elements are
called members
3
Defining a Structure
struct tag {
member 1;
member 2;
:
member m;
};

 struct is the required C keyword


 tag is the name of the structure
 member 1, member 2, … are individual member
declarations

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

◼ Defining structure variables:


struct student a1, a2, a3;

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;

◼ Declares three variables of type struct tag


◼ In this form, tag is optional 7
Accessing member of Structure
◼ The members of a structure are processed
individually, as separate entities
 Each member is a separate variable
◼ A structure member can be accessed by using dot (.)
operator :
variable.member
where variable refers to the name of a structure-type
variable, and member refers to the name of a
member within the structure
◼ Examples:
a1.name, a2.name, a1.roll_number, a3.dob
8
Structure Initialization
◼ A structure variable can be initialized at compile time:
◼ Syntax:
◼ struct structure_name structure_variable={value1,value2,…..valueN}
◼ void main( )
◼ {
◼ struct st-record
◼ {
◼ int weight;
◼ float height:
◼ };
◼ struct st-record student1={ 60,180.75};
◼ struct st-record student2={53,170.60};
◼ }

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;

scanf (“%f %f”, &a.real, &a.cmplex);


scanf (“%f %f”, &b.real, &b.cmplex);

c.real = a.real + b.real;


c.cmplex = a.cmplex + b.cmplex;
printf (“\n %f + %f j”, c.real, c.cmplex);
}
11
Example1
◼ #include <stdio.h>
◼ struct book{
◼ char title[10];
◼ char author[20];
◼ double price;
◼ int pages;
◼ };
◼ int main(){
◼ struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 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;
◼ }

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

◼ Two structure variables can not be compared


for equality or inequality
if (a1 == a2)…… this cannot be done

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

You might also like