structures
15.1 Introduction
-
user defined datatype thatcan store related information (hetero-
-genous datatype) together.
-
Collection of variables of differentdatatypes under a
single name
Declaration
syntax: struct struct-name
2
datatype varnque;
qatype var-name;
3,
EX? struct student
2
int uno;
Char
char
name [20];
course
floatfees,
(20]; 3 members ofstructure student
3,
allocated for the structure when variable
Memory is a
-
is declared.
- 2
ways of declaring variables
struct student struct student
E 2
intr_no; int uno;
char name [20]; Char name [20];
char course
(20]; char course (20];
float fees; floatfees,
3 Studl, stude; 3,
e
struct studentstud,
studz;
e
>2-variables of type
Stenct student <
Typedef Declarations:
-typedef' keyword enables the programmer to create a new
datatype name from existing data
type.
Syntax: typedef existing-data-type new data-type
Page:ol
Ex: typedef int INTEGER;
new name of datatype of int
Usage:INTEGER a; is same as
inta;
-Above concept is used for structures like below
typedef struct student
2
int uno; typed of
Char name [20];
studentA
char course (20];
floatfees,
I stud;
stud si,se; is same as struct student 51,52;
Initialization of structures:
-
assigning the structure member values
during declaration.
2
ways
struct student struct student
2 2
int uno; int uno;
Char name [20]; Char name [20];
char course (20]; char course (20];
floatfees, floatfees,
351 401,"seeta", "ISE", 970004' y
simct
=
student 51: 202, "rama","t"
1950007;
Accessing members of structure.
Reading and writing structure member values.
using i). (dot) operator
ii) -> Carrow operator (pointers)
writing into structure:
struct student 3.2_nO = 01;
2
int uno; strcpy(s.name," Raj");
Char name [20];
char course (20]; strcpy (S. course," ISE");
floatfees, S.fees=
97000;
3,
struct student si Si 01 I Raj I ISE 97000
~_ no name course fees
Page:02
Reading structure members:
out
printf (" Yod", s,r_no); 01
printf("ys", so
name); Raj
printf ("ys", S.
Course); I SE
printf(" Yot", so
fees); 97000
structure to another structure:
copying a
If
Struct studentsi 201," Rabul",=
"ISE",970007;
struct student se;
copying 51 to 32. struct student
2
52 St
perform following
=
above statement will in backend int uno;
SI.U_no;
S2._nO= Char name [20];
char course (20];
Strapy (s2.Name, sl.nque); floatfees,
S2, Course);
strcpy (S2. Course, 3,
Sl.fees;
S2.fees=
Similarly 2 student variables can be compared.
15.2 Nested structures
placing a structure within the other structure is called
Nested structures.
2-ways:
① ② struct student
struct dob
E
int roll-no;
int day; char name [100];
int month;
floatfees;
int
year; struct dob
;
L
struct student int day;
int month;
int roll-no; int
year;
char name [100]; Y date;
floatfees; 3,
struct dob date;
3.
I
accessed method
member of above structure can be
by following
struct students,
Page:03
S.voll_no= 99;
strcpy(s. name," Raj");
s, fees =
97000;
S. date, day: of;
Sodate, month 03; =
S. date. Year = 2023,
15.3
Arrays of structures
Like
away of primitive datatype variables, structure variable away
can also be declared as shown below
Ex:struct student
--
2
int uno;
Char name [20];
char course (20];
floatfees,
3,
of struct student variable be declared
Array can as
struct students [5];
memory Allocation
~_nO nause course fees
S[0]
S[I] 97000
-
s[2]
s[z] 04
- S[1].fees=97000
S[3]. 2nO 04
=
① write read and studentinformation
a cProgram to
display the
and
display same.
include (staio.b)
#
struct student
2
int uno;
Char name [20];
char course (20];
floatfees,
3,
mainc
E
struct student s[5]; Page:04
inti;
printf (" Enter number of students: ");
scanf (" d", In);
for (i =0; i<n; itt)
S
printf ("Enter student yd details");
printf (" Enter roll number"),
scanf(" %d":Is[i].-no);
Printf(" Enter name:");
scant (" "S", S[i].name),
printf(" Enter course:");
scanf (""s" [i].
course);
printf ("Enter fees");
scanf (" yof", Ifees);
3
for(i =0;i<n; itt)
E
Printf(" student God details:");
Printf("Roll-no =%d", s[i].v_no);
printf("Name=ys", s[i]. name);
Printf (" course ys", S(i). course);
=
printf(" fees=%7", s[i], fees);
3
3
15.4 structures and Functions
Passing structures to function may be done in following 3-ways
Passing individual members
-
Passing the entire structure
of
passing the address the structure.
-
Passing individual members
passing individual member to function as call
by value.
2 #include <stdio.4>
typedef struct I struct definition
E
ints;
inty,
YPOINT,
void fun declaration
display (int, int); //
Page:05
int main
E
POINT 41= 22,33;
display (P1.2, P1.y); 1 P1.x & p.y are struct members
return o;
I
and
void display (inta, intb)//<BU, a P1.4 b
p1.y
=
=
a 2 b 3 =
E
=
Printf(" The coordinates of the point are: dd", a, b);
3
Output:
The coordinates of the point are:23
the entire structure
Passing
-structure variable can be passed directly function
to as
argument
Note:structure definition should be declared as global.
3 include (stdio.4)
typedef struct
E
intn;
inty;
3POINT,
> derived datatype (struct paint
void
display (POINT);
main()
E
POINT P1 G2,3}
=
display (P1); //
Passing 41 (struct variable)
return o;
3
void display POINTP) //CBU:P.M=Plax and P.y Ply =
printf(" ,dd", P.2, 4.4);
3
Binge
through pointers
-
Instead of CBV, structure variable can be passed as address
4 include (stdio.h)
#
typed of struct point
G page:of
int x;
int
y,
Y POINT;
int main(>
E
POINT AP;
POINT S
22,37;
=
P IS;
=
//P holds the address of struct variable s
display (P);
return o;
y
void display (POINT *ptr)
2
printf(" dd", Ptr-n,ptr-y);
3 Arrow operator since ptr is a printer
variable
output:
2 3
15.5 Self-Referential structures
reference ofdata
-structures thatcontain a to its same
type is e
in addition to other data, a self-referential structure contains
a
printer to a thatis of
data the same type as thatof structure.
Ex struct node
E
2000
int val; val -> val NULL
next
struct node *next;
1008 2000
3,
-
Pointer to a variable
of struct model
15.6 Unions
-collection of variables of differentdata-types.
stoves information in one field at one time.
any
-
allocated for union is the size of largestmember.
-memory
Declaration: Ex:
union union-name union vehicle
I 2
data type var-name; char chassi-num [307;
data type var-name;
....
char registration num [10];
-
y; 3,
union vehicle vI; Page:07
size of 30
vI= Bytes
- vehicle v can stareither chassi-num
at
or
registration -
num
any
time.
of union members
essing nation
Accessing using
dot (.) operator.
Example:
5 include (stdio.h)
#
typedef struct
E
int x, y,
YPOINT1;
definition
atyedef union Union
int x,y,
3POIN+1;
int main
E
POINT P1 22,34;
=
I POINT P2=G4,57;
illegal with union ata time it can
store value y
POINT P2;
only one
P2.X 4; =
Initialization of
1) unic
P2.4 5; overwrites the team 4
5
memory
= to
Printf ("using structures n
ydy xd",
= =
p1.x, p2.y);
Printf(" using unions x = ed y = d",
(2-4, P2.y);
return 0;
B
output:
structures
using
using unions I, II s 1/ note both values are
same since common
to &
memory y.
i
Differences between structure & Unin
1. Memory Allocation: In a structure, memory is allocated for each member variable separately,
whereas in a union, all the member variables share the same memory location.
2. Size: The size of a structure is the sum of the sizes of its member variables. The size of a union is
equal to the size of its largest member variable.
3. Accessing Members: In a structure, you can access individual members of the structure using the
dot (.) operator. In a union, all the member variables share the same memory location, so only one
member can be accessed at a time.
4. Usage: A structure is typically used to group related data together, while a union is used to
represent different types of data in the same memory location. Page: 08
15.10 Enumerated data type:
-
user defined datatype based on integers.
-
consists of named integer constants, i.e each integer is assigned
with a identifier.
Declaration: enum enumeration -
name a identier, identifiers.... s
0. 3
Example:enum days-of-week (Miday, Tuesday, Wednesday, Thursday,
Friday, Saturday
↓ 4 sunday's default
5
New
data-type values of
name identifiers
the enumerated
Accessing values:
6 include <stdio.4)
#
enum days-of-week (Monday, Tuesday, Wednesday, Thursday,
Friday, saturday, Sundayy;
int main
2
11 Declaration of variable of type dayofweek and assign it a variable
enum days-of-week today-Tuesday;
/print the value of the variable
printf("Today is
.9", today),
return e;
3
out:
Today is
moun
Page:09