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

L31 Structures

The document discusses structures in C programming. It defines structures as a way to group related data of different types together under a single name for convenient handling. It provides examples of defining structures and declaring structure variables. It also describes how to assign values to structure members using the dot operator and initialization methods. The key outcomes are listed as understanding structures, writing programs using structures, understanding arrays of structures, and writing programs using arrays of structures.

Uploaded by

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

L31 Structures

The document discusses structures in C programming. It defines structures as a way to group related data of different types together under a single name for convenient handling. It provides examples of defining structures and declaring structure variables. It also describes how to assign values to structure members using the dot operator and initialization methods. The key outcomes are listed as understanding structures, writing programs using structures, understanding arrays of structures, and writing programs using arrays of structures.

Uploaded by

Ishank Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Structures

Objectives
To learn and appreciate the following concepts

• Basic operations and programs using structures


• Advantages of structures over array

6/4/2022 CSE 1051 Department of CSE 2


Session outcome

At the end of session one will be able to

1. Understand the overall ideology of structures

2. Write programs using structures

3. Understand the array of structures

4. Write programs using array of structures

6/4/2022 CSE 1051 Department of CSE 3


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}

6/4/2022 CSE 1051 Department of CSE 4


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

6/4/2022 CSE 1051 Department of CSE 5


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 defined by the keyword struct.

6/4/2022 CSE 1051 Department of CSE 6


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.

6/4/2022 CSE 1051 Department of CSE 7


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.

6/4/2022 CSE 1051 Department of CSE 8


Structure Definition - Syntax
The general format of a structure definition

struct structure_name
{
data_type member1;
data_type member2;

};

6/4/2022 CSE 1051 Department of CSE 9


Structure Definition - Examples
• Example:

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

6/4/2022 CSE 1051 Department of CSE 10


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];
char Address[30]; The “Employee”
int Age;
float Basic; structure has 6
float DA; members
float NetSalary;
};

6/4/2022 CSE 1051 Department of CSE 11


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.

6/4/2022 CSE 1051 Department of CSE 12


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.

6/4/2022 CSE 1051 Department of CSE 13


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.

6/4/2022 CSE 1051 Department of CSE 14


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;

6/4/2022 CSE 1051 Department of CSE 15


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;
s1.rollno = 1335; int age;
char name[20];
s1.age = 18; float height;
s1.height = 5.8; }s1;

Reading the values into members:


scanf(“%s %d %f %f”, s1.name, &s1.age, &s1.rollno, &s1.height);

6/4/2022 CSE 1051 Department of CSE 17


Structure Initialization Methods
There is one-to-one correspondence between the members and their
initializing values.

1. Without tag name.

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

}
6/4/2022 CSE 1051 Department of CSE 18
Structure Initialization Methods

2. Using tag name.

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

6/4/2022 CSE 1051 Department of CSE 19


Structure Initialization Methods
3. Using a tag name and defined outside the function.

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


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

//output
printf(“%s %s %d %f”, b1.title, b1.author, b1.pages, b1.price);
return 0;
}
6/4/2022 CSE 1051 Department of CSE 21
Summary

• Structure Basics

• Member accessing using dot operator

• Simple problems using structures

6/4/2022 CSE 1051 Department of CSE 22

You might also like