You are on page 1of 20

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 :Initialization,accessing &assignment
Teaching Aids :PPTs,Animation
CM304.77 1
Recap
 Define a structure?
 Explain structure definition with an example?
 How to declare a structure variable?
 Is the memory allocated for the members
when the structure is defined?
 Can you assign values to members within the
structure?
 How to combine structure definition and
structure variable declaration ?
CM304.77 2
Objectives

On completion of this period, you would be able


to know ...

 Initialize structure members.


 Access the structure members.
 Assign values to members.
 Read data for the members from the keyboard.

CM304.77 3
Initialization
 Members can be initialized at the time of defining
the structure or declaration of structure variable
Syntax :
struct tagname variable={value1,value2,…valuen};
 Example
Method 1) struct emp e={1000,60000.50,’M’};

Method 2) struct emp e={1000,


60000.50,
‘M’
};

CM304.77 4
Initialization Contd…..
Method 3) struct emp
{
int empno;
float sal;
char gender;
}e={1000,60000.50,’M’};
initialized during
difinition

empno sal
gender
1000 60000.50 M
e

Fig 77.1 CM304.77 5


Invalid Example

struct emp e;
e={101,5000.5,’M’};
 Members should be initialized at the time of
declaration.

CM304.77 6
Accessing Structure Members
 Dot(.) operator is used to access members

Syntax:

Structurevariable.membername;

Example

e.empno; gives content of empno


e.sal; gives content of sal
e.gender; gives content of gender
CM304.77 7
Example

CM304.77 8
Structure Assignment

 Members can be initialized separately.


 Assigning values to members.

e.empno=5001;
e.sal=1000;
e.gender=‘M’;

CM304.77 9
Assigning One Structure Variable to
Another
 Contents of one structure variable can be
copied to another variable.
Example
struct sample
{
char a;
int b;
float c;
};
struct sample s1,s2;
CM304.77 10
Assigning One Structure Variable to
Another
s1.a=‘A’;
s1.b=100; Contd…
s1.c=100.5;
a b c
s1 A 100 100.5
Fig 77.2
s2=s1;
a b c

s2 A 100 100.5

Fig 77.3

CM304.77 11
Example

CM304.77 12
Reading data for the members

 Different ways to give data for the members:


 1) Members can be initialized at the time of
defining the structure or variable declaration
 2) Values can be assigned to members separately
 3) Data can also be given for the members from
the keyboard

CM304.77 13
Ex:
Example

CM304.77 14
Summary

In this class, you have learnt about

 Initialization of structure members.

 Accessing structure members.

 Assigning values to members.

 Read data for the members from the keyboard.

CM304.77 15
Quiz

1)When accessing a structure member the

identifier to the left of the dot operator is

a)a structure member

b)a structure tag

c)a structure variable

d)the keyword struct

CM304.77 16
Quiz

1)When accessing a structure member the

identifier to the left of the dot operator is

a)a structure member

b)a structure tag

c)a structure variable

d)the keyword struct

CM304.77 17
Quiz

2)The operator used to access the structure


member is

a)*(star)

b) &(ampersand)

c) .(dot)

d) [](brackest)

CM304.77 18
Quiz

2)The operator used to access the structure


member is

a)*(star)

b) &(ampersand)

c) .(dot)

d) [](brackest)

CM304.77 19
Frequently Asked Questions

1. Explain initialization of a structure with an


example.
2. How do you assign values to members of the
structure.
3. Explain how to access structure members.
4. Explain how to copy contents of one structure
variable to another.

CM304.77 20

You might also like