0% found this document useful (0 votes)
0 views10 pages

Structures

The document explains the concept of structures in C programming, which allows for the grouping of related variables of different data types. It includes syntax for defining structures, declaring structure variables, and accessing structure elements using the dot operator. Additionally, it introduces arrays of structures for managing multiple entities efficiently.

Uploaded by

Shirshankit tej
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)
0 views10 pages

Structures

The document explains the concept of structures in C programming, which allows for the grouping of related variables of different data types. It includes syntax for defining structures, declaring structure variables, and accessing structure elements using the dot operator. Additionally, it introduces arrays of structures for managing multiple entities efficiently.

Uploaded by

Shirshankit tej
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

Data Structures and Algorithms

21CSC201J
Declaring Structure and accessing
• There is a need to store multiple logically related elements under one roof.
For instance, an employee’s details like name, employee number, and
designation need to be stored together. In such cases, the C language
provides structures to do the job for us
A structure can be defined as a single entity holding variables of different data
types that are logically related to each other. All the data members inside a
structure are accessible to the functions defined outside the structure. To access
the data members in the main function, you need to create a structure variable.
Syntax to Define a Structure in C
struct structName
{
// structure definition
Data_type1 member_name1;
Data_type2 member_name2;
Data_type2 member_name2;
};
Example
struct bookStore
{
// structure definition
char storeName
int totalBooks;
char storeLicense[20];
} storeA, storeB; // structure variables
Declaration of Structure Variables Separately
This way of creating structure variables is preferred when multiple
variables are required to be declared. The structure variables are declared
outside the structure.
Syntax:
struct structName
{
// structure definition
Data_type1 member_name1;
Data_type2 member_name2;
Data_type2 member_name2;
};
struct structName struct_var1, struct_var2;
• How to Access Structure Elements?

• The members of a structure are accessed outside the structure by the


structure variables using the dot operator (.). The following syntax is used
to access any member of a structure by its variable:
• Syntax

• [Link]
Array of Structures in C
An array of structures in C can be defined as the collection of multiple structures
variables where each variable contains information about different entities. The
array of structures in C are used to store information about multiple entities of
different data types. The array of structures is also known as the collection of
structures.
Let's see an example of an array of structures that stores information of 5 students
and prints it.
Thank you

You might also like