Lecture 11 Structures
Lecture 11 Structures
LECTURE 11
C - STRUCTURES
❖ C arrays allow you to define type of variables that can
hold several data items of the same kind but structure
is another user defined data type available in C
programming, which allows you to combine data items of
different kinds.
❖ Structure is the collection of variables of different types
under a single name for better handling.
❖ Structures are used to represent a record, Suppose you
want to keep track of your books in a library. You might
want to track the following attributes about each book:
➢ Title
➢ Author
➢ Subject
➢ Book ID
DEFINING A STRUCTURE
❏ To define a structure, you must use the struct statement.
The struct statement defines a new data type, with more
than one member for your program.
❏ The format of the struct statement is this:
Member
definition
Example 2
structure
variables
➢ Data types can be int, char, float, double and long double
etc.
BELOW TABLE EXPLAINS FOLLOWING CONCEPTS IN C
STRUCTURE
➢ How to declare a C structure?
➢ How to initialize a C structure?
➢ How to access the members of a C structure?
ACCESSING MEMBERS OF A STRUCTURE
➢ Structure can be accessed in 2 ways. They are,
○ Using normal structure variable
○ Using pointer variable
➢ There are two types of operators used for accessing
members of a structure.
○ Member operator(.)
○ Structure pointer operator(->)
➢ Dot(.) operator is used to access the data using normal
structure variable.
➢ Arrow (->) is used to access the data using pointer
variable.
➢ We already have learnt how to access structure data
using normal variable. So, we are showing here how to
access structure data using pointer variable.
POINTERS TO STRUCTURES
EXAMPLE PROGRAM FOR C STRUCTURE
This program is used to store and access “id, name
and percentage” for one student. We can also store
and access these data for many students using
array of structures.
Output:
Id is: 1
Name is: Raju
Percentage is: 86.500000
EXAMPLE PROGRAM-ARRAY OF STRUCTURES
This program is used to store and access “id, name and
percentage” for 3 students. Structure array is used in
this program to store and display records for many
students. You can store “n” number of students record
by declaring structure variable as ‘struct student
record[n]“, where n can be 1000 or 5000 etc.
Output:
Records of STUDENT : 1
Id is: 1
Name is: Raju
Percentage is: 86.500000
Records of STUDENT : 2
Id is: 2
Name is: Surendren
Percentage is: 90.500000
Records of STUDENT : 3
Id is: 3 Name is: Thiyagu
Percentage is: 81.500000
EXAMPLE PROGRAM OF STRUCTURE
Write a C program to add two distances entered by user. Measurement
of distance should be in inch and feet.(Note: 12 inches = 1 foot)
PASSING STRUCTURE TO FUNCTION
Output:
Enter student's name: Kevin
Enter roll number: 149
Name: Kevin
Roll: 149
Passing structure variable
EXAMPLE – PASSING STRUCTURE TO FUNCTION BY VALUE
In this program, the whole structure is passed to
another function by value. It means the whole
structure is passed to another function with all
members and their values. So, this structure can
be accessed from called function. This concept is
very useful while writing very big programs in C.
Output:
Id is: 1
Name is: Raju
Percentage is: 86.500000
EXAMPLE – PASSING
STRUCTURE TO FUNCTION
BY VALUE
PASSING STRUCTURE TO FUNCTION BY ADDRESS/REFERENCE
Output:
First distance
Enter feet: 12
Enter inch: 6.8
Second distance
Enter feet: 5
Enter inch: 7.5
Output:
Id is: 1
Name is: Raju
Percentage is: 86.500000
EXAMPLE–PASSING
STRUCTURE TO FUNCTION BY
ADDRESS/REFERENCE
EXAMPLE PROGRAM TO DECLARE A STRUCTURE VARIABLE AS
GLOBAL
Structure variables also can be declared as global
variables as we declare other variables in C. So,
When a structure variable is declared as global,
then it is visible to all the functions in a program.
In this scenario, we don’t need to pass the
structure to any function separately.
Output:
Id is: 1
Name is: Raju
Percentage is: 86.500000
COPY A STRUCTURE
▪ There are many methods to
copy one structure to
another structure in C.
• We can copy using direct
assignment of one structure
to another structure or
• we can use C inbuilt
function “memcpy()” or
• we can copy by individual
structure members.
Output:
Records of STUDENT1 - record1 structure
Id : 1
Name : Raju
Percentage : 90.500000
Records of STUDENT1 – Direct copy from record1
Id : 1
Name : Raju
Percentage : 90.500000
Records of STUDENT1 – copied from record1 using
memcpy
Id : 1
Name : Raju
Percentage : 90.500000
Records of STUDENT1 – Copied individual members from
record1
Id : 1
Name : Raju
Percentage : 90.500000
KEYWORD TYPEDEF WHILE USING STRUCTURE
Output:
size of structure1 in bytes : 16