Vous êtes sur la page 1sur 18

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 Min
Sub Topic :Array of structures&self referential
structures
Teaching Aids :PPTs, Animation
CM304.80 1
Recap

 Explain how to pass structure members as


arguments to function.
 Explain how to pass an entire structure as
argument to function.
 Explain how to pass address of structure as
argument to function.
 What is an array?

CM304.80 2
Objectives

On completion of this period, you would be able


to know…
 Declare array of structures.
 Define structure containing pointers.
 Define Self referential structures.

CM304.80 3
Array of Structures

 Can hold structure variables of same type


 Declaration
Must be +ve integer
– Syntax
– struct tagname arrayname[size];
– Memory allocated=size*no.of bytes occupied
by each record

CM304.80 4
Array of Structures
Contd…..
 Usage
 arrayname[i].membername;
 where i varies from 0 to size-1
 Example
struct student
{
int empno;
float sal;
char gender;
};
CM304.80 5
Array of Structures Contd…..
 struct student s[5];
 Memory allocated=5*7=35 bytes

empno sal gender


0 S[0]
1 S[1]
Structure
2 S[2]
variables
3 S
[3]
S[4]
4
S[4].gender
S[4].empn S[4].sal
o Fig 5.4.8 CM304.80 6
Example

CM304.80 7
Example
Contd…..

CM304.80 8
Structure containing Pointers

 A pointer can also be used as member of the


structure.
 Example
struct sample
{
int *p1; /* p1 is a pointer*/
float *p2; /* p2 is a pointer*/
char c /* c is an ordinary variable*/
};

CM304.80 9
Example

CM304.80 10
Self Referential Structures

 One of the members is a pointer of its own


type.
 This is also known as linked list.
 Syntax:
struct tagname
{
datatype member1; Pointer variable of
datatype member2; Same type
----------------------;
struct tagname *pointervariable;
};

CM304.80 11
Example

struct node
{
int data;
struct node *link; /* link is a pointer of type struct node*/
};

CM304.80 12
Example

CM304.80 13
Summary

In this class, you have learnt about …


 Array of structures is a collections structure
variables of same type.
 Members of the structure may be ordinary
variables, arrays, structures or pointers.
 If one of the members of the structure is a
pointer of its own type is known as Self
referential structure.
CM304.80 14
Quiz

1)If one or more members of a structure are


pointing to the same structure, the structure is
known as
a) Nested structure
b) Invalid structure
c) Self-referential structure
d) None

CM304.80 15
Quiz

1)If one or more members of a structure are


pointing to the same structure,the structure is
known as
a) Nested structure
b) Invalid structure
c) Self-referential structure
d) None

CM304.80 16
Frequently Asked Questions

1. Explain array of structures with an example?


2. Define self referential structure?give one
example?

CM304.80 17
Assignment

 Write a program for array of employees.


Employee structure contains empno, ename, sal,
designation and qualification fields. Read and
display contents of the structure using array of

structures.

CM304.80 18