P. 1
CM304

CM304

|Views: 8|Likes:
Published by api-3849444

More info:

Published by: api-3849444 on Oct 19, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

DEPARTMENT OF TECHNICAL EDUCATION ANDHRA PRADESH

Name Designation Branch Institute Year/Semester Subject Subject Code Topic Duration Sub Topic Teaching Aids :M.Subramanyam :Senior Lecturer :Computer Engg. :Q.Q.Govt.Polytechnic,Hyderabad. :III Semester :UNIX & C :CM-304 :Structures :50 Min :Initialization,accessing &assignment :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
Method 3) struct emp { int empno; float sal; char gender; }e={1000,60000.50,’M’};

Contd…..

initialized during difinition empno gender 1000 sal

e

60000.50

M
5

Fig 77.1

CM304.77

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 s2=s1;
a

100
Fig 77.2

100.5

b

c

s2

A

100
Fig 77.3
CM304.77

100.5

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're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->