Vous êtes sur la page 1sur 22

DEPARTMENT OF TECHNICAL EDUCATION

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

 How do you initialize a structure?

 How do you assign values to structure


members?
 How do you access structure members?

CM304.78 2
Objectives

On completion of this period ,you would be able to

 Find the size of the structure

 Define structure within structure

 Declare a Pointer to structure

CM304.78 3
Sizeof the Structure

 Sum of sizes of its members


 Example
struct emp
{
int empno;
float sal;
char gender;
};
struct emp e;
sizeof(e) or sizeof(struct emp)=2+4+1=7 bytes

CM304.78 4
Sizeof the Structure Contd…..

empno sal gender

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

 Complex data types can be created

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…..

 struct sample *p;/*p is a pointer to structure*/


 p=&s; /*starting address of s is stored in p*/
 assume 1000 is starting address of s

p 1000 1001 1003


1000 S

a b c
Fig 78.3
CM304.78 13
Example Contd…..

&p->a gives address of a i.e 1000


&p->b gives address of b i.e 1001
&p->c gives address of c i.e 1003
p->a gives content of a i.e A
p->b gives content of b i.e 100
p->c gives content of c i.e 5.5

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

1)The operator exclusively used with pointer to


structure is
a) .

b) []

c) ->

d) *

CM304.78 17
Contd…..

2)struct account
{
int accno;
char acctype;
char cname[25];
float bal;
};

CM304.78 18
Contd…..

struct account cust,*ptr;


ptr=&cust;
For referring to cname using pointer ptr,we would
write
a)ptr.cname;
b) ptr->cname;
c)ptr.name[20]
d) ptr->cname[25]

CM304.78 19
Contd…..

3)If one or more members of a structure are other


structures, the structure is known as
a) nested structure
b) self-referential structure
c) array of structures
d) none

CM304.78 20
Frequently Asked Questions

 Explain nested structure with an example?

 Explain the concept of pointers to structures with

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

Vous aimerez peut-être aussi