Module 5 Structures
Module 5 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,
floatfees,
(20]; 3 members ofstructure student
3,
is declared.
- 2
ways of declaring variables
struct studentstud,
studz;
e
>2-variables of type
Stenct student <
Typedef Declarations:
Usage:INTEGER a; is same as
inta;
Initialization of structures:
-
2
ways
struct student struct student
2 2
int uno; int uno;
Char name [20]; Char name [20];
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
If
Struct studentsi 201," Rabul",=
"ISE",970007;
struct student se;
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=
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;
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
memory Allocation
~_nO nause course fees
S[0]
S[I] 97000
-
s[2]
s[z] 04
- S[1].fees=97000
S[3]. 2nO 04
=
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);
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
=
Output:
The coordinates of the point are:23
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
Binge
through pointers
-
Instead of CBV, structure variable can be passed as address
4 include (stdio.h)
#
int main(>
E
POINT AP;
POINT S
22,37;
=
P IS;
=
//P holds the address of struct variable s
display (P);
return o;
y
output:
2 3
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
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
output:
structures
using
using unions I, II s 1/ note both values are
same since common
to &
memory y.
i
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.
New
data-type values of
name identifiers
the enumerated
Accessing values:
6 include <stdio.4)
#
printf("Today is
.9", today),
return e;
3
out:
Today is
moun
Page:09