CM304
CM304
CM304
ANDHRA PRADESH
Name :M.Subramanyam
Designation :Senior Lecturer
Branch :Computer Engg.
Institute :Q.Q.Govt.Polytechnic,Hyderabad.
Year/Semester :III Semester
Subject :Unix & C
Subject Code :CM-304
Topic :Structures
Duration :50 Mts
Sub Topic :Size,Nested &Pointer to Structure
Teaching Aids :PPTs,Animation
CM304.78 1
Recap
CM304.78 2
Objectives
CM304.78 3
Sizeof the Structure
CM304.78 4
Sizeof the Structure Contd…..
e
2 bytes 4 bytes 1 byte
Fig 78.1
sizeof(e)=sizeof(empno)+sizeof(sal)+sizeof(gender)
sizeof(e)=2+4+1=7bytes
CM304.78 5
Example
CM304.78 6
Nested Structure
Structure in a structure is called nested structure
Inner structure must be defined first
Outer structure is defined next
Inner structure members can be accessed
Outerstructurename.innerstructurename.member
CM304.78 7
Example
CM304.78 8
Nested Structure Contd…..
do
pin sname d bm y
Inner structure
Outer structure
Fig 78.2
CM304.78 9
Example
CM304.78 10
Pointer to Structure
Pointer variable that can hold stating address
of a structure variable
Declaration
Syntax:
– struct tagname *pointervariable(s);
Address of the member
– &pointervariable->member
Content of the member
– pointer variable->member
CM304.78 11
Example
struct sample
{
char a;
int b;
float c;
};
struct sample s={‘A’,100,5.5};
CM304.78 12
Example Contd…..
a b c
Fig 78.3
CM304.78 13
Example Contd…..
CM304.78 14
Example
CM304.78 15
Summary
In this class you have learnt
Size of the structure is sum of sizes of its
members
One Structure can be defined within another
structure
Pointer can be declared to a structure and
members are accessed using a pointer
CM304.78 16
Quiz
b) []
c) ->
d) *
CM304.78 17
Contd…..
2)struct account
{
int accno;
char acctype;
char cname[25];
float bal;
};
CM304.78 18
Contd…..
CM304.78 19
Contd…..
CM304.78 20
Frequently Asked Questions
an example?
CM304.78 21
Assignments
Write a program using pointer to structure.Define the
structure with tagname state with fields
statename,number of districts and total population. Read
and display the data using pointer to structure
Write a program for employee structure. define structure
with tagname address with fields Hno,street,Mandal and
district. Define structure with tagname emp with fields
empno, ename, sal, designation and struct address add.
address is inner structure and emp is outer
structure.Read and display the data
CM304.78 22