0% found this document useful (0 votes)
21 views24 pages

Understanding Structures in C Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views24 pages

Understanding Structures in C Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

STRUCTURES

SOMESH NANDI
DEPARTMENT OF AIML -RVCE

10/22/2024 1
Introduction
• Here is the Problem Statement………………………………
• I have a Car Showroom and I want to store the details of every car in software ?
How?
• I need to define the variables for each car to store its specifications that of
different data types

• Suppose if I have 1000 Cars, is this method is user friendly? – Of course, No


Definition of Structure
How to Create a
Structure?
Syntax
• To create a
Example
structure in C,
the struct keyword
is used followed by
the tag name of the
structure.
• Then the body of
the structure is
defined, in which
the required data
members (primitive
or user-defined data
types) are added.

10/22/2024 4
Sample Footer Text How to Declare Structure
Variables?
• There are two
ways to
declare
variables for
structure in C
language:

10/22/2024 5
How to Declare Structure
Variables?
[Link] Way:
• As we create a
structure in C, we
have created a user-
defined data type.
So this data type
can be treated as
the primitive data
type while declaring
a variable for that
structure.

10/22/2024 6
Type def declaration

• The typedef is a keyword that is used to provide existing


data types with a new name. The C typedef keyword is used
to redefine the name of already existing data types.
• When names of datatypes become difficult to use in
programs, typedef is used with user-defined datatypes,
which behave similarly to defining an alias for commands.

• Eg :typedef int AIML


• AIML N=5
Type def declaration
Type def declaration
Initialize Structure Members

• Initializing a structure member means assigning values to the


structure members according to their respective data types.
• But declaration doesn't allocate memory for the structure.
When we declare a variable for a structure, only then is the
memory allocated to that structure variable.
• Hence, assigning value to something that doesn't have
memory is the same as serving food without a plate, which
isn't a good idea! In short, structure members cannot be
initialized during the declaration. For example:
Initialize Structure Members

• There are three ways to initialize


structure members:
• Using dot '.' operator
• Using curly braces ‘{}’
• Designated initializers
Initialization of Structures

Initialize Structure
Members – Using
dot(.) Operator

• Using the dot (.) operator, we can


access any structure member and
then initialize or assign its value
according to its data type.
• Syntax:
• struct structure_name
variable_name
• variable_name.member = value;

10/22/2024 12
Using curly braces ‘{}’

• If we want to
initialize all the
members during
the structure
variable
declaration, we can
declare using curly
braces.
• Syntax:
• struct
stucture_name v1 =
{value, value,
value, ..};
10/22/2024 13
Accessing the Members of Structures

Access Structure Members – Using


dot(.) Operator

• Using the dot (.) operator, we


can access any structure
member and then initialize or
assign its value according to its
data type.
• Syntax:
• struct structure_name
variable_name
• variable_name.member = value;

10/22/2024 14
Copying and Comparing
Structures

• We can assign
structure to another
structure of same
type.
• Two variables of the
same structure type
can be copied the
same way as
ordinary variables.
If b1 and b2 are two
book variables, then
the following
statements are
valid:

10/22/2024
• b1 = b2; b2 = b1 15
Array of Structures
• The array of structures can also
be described as a collection of
structure variables.
• The main advantage of an array
is we can represent multiple
values with a single variable.
So the reusability of code
improves; also, readability is
increased.
• If there is no array of
structures, we need to store
many values in multiple
structure variables, which is
redundant

10/22/2024 16
Array of Structures- Example
• S is array of structure student having size 10 which means it could
store information of 10 different variables of type student.
• So we don't need to take 10 different variables instead we could use
array of structure student.

10/22/2024 17
Structures and Functions
• Structures can be passed as arguments to the functions.
This can be done in three ways. They are,
• Passing the members of the structures as an argument.
• Passing the entire structure as an argument.
• Passing the address of the structure as arguments.
Passing the Induvial Members
• The dot (.) operator is used to access the individual members of
the structure and pass them to the function.
• Eg: Let us create a structure to hold the details of a student,
such as the name of the student, roll number, and marks, and
print out just the roll number and marks using a function.
Passing the entire structure to the function is unnecessary
when we want to print only a few structure members
Passing the Induvial Members
we created a structure to hold
the name, roll number, and
percentage of the student.
The input from the user is stored
in the structure. A function
named display() is created,
which takes the roll number and
the percentage of the student as
the parameter.
Using the dot (.) operator, we
accessed the member of the
structure and passed it to the
function.
Passing the Entire Structure
• You can pass a structure variable to a function as argument like we
pass any other variable to a function.
• Structure variable is passed using call by value.(Copy of the Each
Member is made)
• Instead of writing the members in display function, write the structure
name in the pervious example.
Passing the Structure through Pointers
•Passing the parameter as a value will make a copy of the
structure variable, passing it to the function.
•Imagine we have a structure with a huge number of structure
members. Making a copy of all the members and passing it to
the function takes a lot of time and consumes a lot of memory.
To overcome this problem, we can pass the address of the
structure.
•Pointers are the variables that hold the address of other
variables. We can use pointers to pass the structure by
reference
Passing the Structure through Pointers

• structure named car and a function


named print_struct() are defined.
The structure stores the model
name, seating capacity, and the fuel
type of the vehicle.
• In the main() function, we created a
structure variable named tata and
stored the values. Later the address
of the structure is passed into
the print_struct() function, which
prints the details entered by the
user.
• The address is passed using the
address operator ampersand (&). To
access the pointer members, we use
the arrow operator -> operator.

10/22/2024 23
Passing the Structure through Pointers
•Passing the parameter as a value will make a copy of the
structure variable, passing it to the function.
•Imagine we have a structure with a huge number of structure
members. Making a copy of all the members and passing it to
the function takes a lot of time and consumes a lot of memory.
To overcome this problem, we can pass the address of the
structure.
•Pointers are the variables that hold the address of other
variables. We can use pointers to pass the structure by
reference

You might also like