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

12 Structures

Uploaded by

ayushd172005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

12 Structures

Uploaded by

ayushd172005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Structures

05/23/2024 CSE 1001 Department of CSE 1


Introduction
 We’ve seen variables of simple data types, such as float,
char, and int.

We saw derived data type, arrays to store group of


related data.

 Variables of such types represent one item of


information: a height, an amount, a count, or group of
item with same data type: list[10] and so on.

 But, these basic types does not support the storage of


compound data.
Eg. Student {name, address, age, sem, branch}
05/23/2024 CSE 1001 Department of CSE 2
Introduction
 C provides facility to define one’s own type
(user-defined) that may be a composite of basic
types (int, char, double, etc) and other user-
defined types.

Structures

CSE 1001 Department of CSE 05/23/2024 3


Introduction
 Definition:
 collection of one or more variables, possibly of
different types, grouped together under a single
name for convenient handling

• A structure type in C is called struct.

CSE 1001 Department of CSE 05/23/2024 4


Structures
• Structures hold data that belong together.
• Examples:
Student record: student id, name, branch, gender, start
year, …
Bank account: account number, name, address, balance,

Address book: name, address, telephone number, …
• In database applications, structures are called records.

CSE 1001 Department of CSE 05/23/2024 5


Structure versus Array

• A struct is heterogeneous, that means it can be


composed of data of different types.

• In contrast, array is homogeneous since it can


contain only data of the same type.

CSE 1001 Department of CSE 05/23/2024 6


Structure Definition - Syntax
The general format of a structure definition

struct structure_name
{
data_type member1;
data_type member2;

};

CSE 1001 Department of CSE 05/23/2024 7


Structure Definition - Examples
• Example:

struct Date
{
Members of the
int day;
structure Date
int month;
int year;
};

CSE 1001 Department of CSE 05/23/2024 8


struct examples
• Examples:
i) struct StudentInfo{
int Id; The “StudentInfo”
int age; structure has 4 members
char Gender; of different types.
double CGA;
};
ii)struct Employee{
char Name[15];
The “Employee”
char Address[30];
structure has 6
int Age;
members
float Basic;
float DA;
float NetSalary;
05/23/2024 }; CSE 1001 Department of CSE 9
Important Points Regarding
Structures

• Definition of Structure reserves no space.

• It is nothing but the “ Template / Map /


Shape ” of the structure .

• Memory is created, very first time when a


variable of structure type is created /
Instance is created.

CSE 1001 Department of CSE 05/23/2024 10


Declaring Structure Variables
• Declaration of a variable of struct type using struct tag name, after
structure definition:
<struct-type> <identifier_list>;

• Example:
StudentInfo Student1, Student2;

Name Name
Student1 Id Gender Id Gender Student2
Dept Dept

Student1 and Student2 are variables of StudentInfo type.


05/23/2024 CSE 1001 Department of CSE 11
Declaring Structure Variables
Declare them at the time of structure definition:

struct student
{
int rollno;
int age;
char name[10];
float height;
}s1, s2, s3; /* Defines 3 variables of type student */
Members of a structure themselves
are not variables. i.e. rollno alone does
not have any value or meaning.

CSE 1001 Department of CSE 05/23/2024 12


Member or dot operator
 The link between member and a structure
variable is established using the member
operator ‘.’ which is also known as ‘dot
operator’
<struct-variable>.<member_name>

e.g.: student s1; // s1 is a variable of type


//student structure.
s1. rollno;
s1. age;
s1. name;

CSE 1001 Department of CSE 05/23/2024 13


Ex: Member accessing using dot operator
• Example:
StudentRecord Student1; //Student1 is a variable of type
//StudentRecord
strcpy(Student1.Name, "Chan Tai Man"); Student1
Student1.Id = 12345;
strcpy(Student1.Dept, "COMP"); Name
Student1.gender = 'M'; Id Gender
printf("The student is “);
switch (Student1.gender){ Dept
case 'F': cout << "Ms. "; break;
case 'M': cout << "Mr. "; break; Chan Tai Man
}
printf(“%s \n”, Student1.Name); 12345 M
COMP
05/23/2024 CSE 1001 Department of CSE 14
Assigning values to members
Different ways to assign values to the members of a structure:

Assigning string:
strcpy(s1.name, “Rama”);
struct student
{
Assignment statement: int rollno;
int age;
s1.rollno = 1335; char name[20];
s1.age = 18; float height;
}s1;
s1.height = 5.8;

Reading the values into members:


scanf(“%s %d %d %f”, s1.name, &s1.age, &s1.rollno,
&s1.height);
CSE 1001 Department of CSE 05/23/2024 15
Structure Initialization Methods

struct Student
int main ( ) {
{ int rollno;
struct Student int age;
{ } s1={20, 21};
int rollno;
int age; main ( )
}; {
Student s1={20, 21}; Student s2={21, 21};
Student s2={21, 21}; …
} …
}

CSE 1001 Department of CSE 05/23/2024 16


Structure: Example
struct Book { // definition
char title[20];
char author[15];
int pages;
float price;
};
int main( ){
struct Book b1;
printf(“Input values”);
scanf(“%s %s %d %f”, b1.title, b1.author, &b1.pages,
&b1.price);
//output
printf(“%s %s %d %f”, b1.title, b1.author, b1.pages,

b1.price);
return 0;
} CSE 1001 Department of CSE 05/23/2024 17
Structure: Example
struct Book { // definition
char title[20];
char author[15];
int pages;
float price;
};
int main( ){
struct Book b1;
printf(“Input values”);
gets(b1.title); gets(b1.author);
scanf(“%d %f”, &b1.pages, &b1.price);
//output
printf(“%s %s %d %f”, b1.title, b1.author, b1.pages,

b1.price);
return 0;
} CSE 1001 Department of CSE 05/23/2024 18

You might also like