You are on page 1of 21

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 :Structure definition and Declaration
Teaching Aids :PPTs, Animation
CM304.76 1
Recap

1. What is an array?

2. What do you mean by homogeneous?

3. What are the different types of arrays?

4. Can you store mixed type of values in an


array?

CM304.76 2
Objectives

On completion of this period, you would be able


to know …

 Understand structures
 Define the structures
 Declare the structure variables

CM304.76 3
Structure

 It is a collection of data elements which are of


different types that are logically grouped
together.
 They are referenced under same name.

 It is heterogeneous.

 Data elements are stored in contiguous memory


locations.
CM304.76 4
Defining a Structure

Keyword
Syntax User defined word
struct tagname
{
datatype member1; Fields/attributes
datatype member2;
datatype member3;
- - - - - - - - - - - - - - - - - --------;
datatype membern;
}; End of the structure definition

CM304.76 5
Structures Contd…

 Memory is not allocated for the members when the


structure is defined.
 Structure definition informs the compiler about
members of structure.
 When program contains several structures they are
distinguished by using tagname.

CM304.76 6
Structures Contd…

 A structure is a way of grouping several pieces

of related data together.

 It can use other structures, arrays, pointers or

unions as some of its members.

CM304.76 7
Structure Example

struct emp
{
int empno;
float sal;
char gender;
};
empno, sal and gender are called members of
structure

CM304.76 8
Invalid Example
struct emp
{
int empno=10; can’t be initialized
float sal =60000.0;
char gender=‘M’;
};
 Members can’t be initialised within the structure
definition.
 Since memory is not allocated for the members
when the structure is defined.

CM304.76 9
Declaration of Structure Variable(s)
 Once the structure is defined ,you can declare a
structure variable

Syntax:
struct tagname
structurevariable(s);
struct emp e;
empno sal gender
e
Fig 76.1
struct emp is datatype called derived datatype

CM304.76 10
Structures Contd…..

 When structure variable is declared memory is

allocated for members of structure.

CM304.76 11
Combined Definition & Declaration

 Structure definition and Structure variable


declaration can be combined together.
struct emp /*structure definition*/
{
int empno;
float sal;
char gender;
} s; Structure variable
CM304.76 12
Combined Definition & Declaration
Contd…..
struct Tagname is optional
{
int empno;
float sal;
char gender;
} s;
 Tagname is optional when structure variable is
declared at the time of defining the structure
itself.

CM304.76 13
Summary

In this period, you have learnt about


 Structure is a collection of variables of different
types.
 Memory is allocated for the members when the
structure variable is declared.
 Members can’t be initialized within the structure.
 Structure definition and variable declaration can
be combined together.
CM304.76 14
Quiz

1)The keyword used to define a structure is


a) Stru
b) Struct
c) Structure
d) St

CM304.76 15
Quiz

1)The keyword used to define a structure is


a) Stru
b) Struct
c) Structure
d) St

CM304.76 16
Quiz

2) Structure definition

a) Describes the prototype

b) Creates structure variable

c) Defines the structure function

d) None

CM304.76 17
Quiz

2) Structure definition

a) Describes the prototype

b) Creates structure variable

c) Defines the structure function

d) None

CM304.76 18
Quiz

3) A structure can have

a) Pointers as its members

b) Scalar data types as its members

c) Structure as its member

d) All the above

CM304.76 19
Quiz

3) A structure can have

a) Pointers as its members

b) Scalar data types as its members

c) Structure as its member

d) All the above

CM304.76 20
Frequently Asked Questions

1. Define a structure.

2. Explain the declaration of a structure with an

example.

CM304.76 21

You might also like