c-19

20
Lecture 19 Lecture 19 Version 1.0 Version 1.0 Structure Structure

Upload: rushdi-shams

Post on 19-Jan-2015

37 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C-19

Lecture 19Lecture 19Version 1.0Version 1.0

StructureStructure

Page 2: C-19

2Rushdi Shams, Dept of CSE, KUET, Bangladesh

What is StructureWhat is Structure

a ‘book’ is a collection of things such a ‘book’ is a collection of things such as title, author, call number, as title, author, call number, publisher, number of pages, date of publisher, number of pages, date of publication, etc publication, etc

all this data is dissimilar all this data is dissimilar author is a string, whereas number author is a string, whereas number

of pages is an integer of pages is an integer

Page 3: C-19

3Rushdi Shams, Dept of CSE, KUET, Bangladesh

What is StructureWhat is Structure

For dealing with such collections, C For dealing with such collections, C provides a data type called ‘structure’ provides a data type called ‘structure’

A structure gathers together, different A structure gathers together, different atoms of information that comprise a atoms of information that comprise a given entity given entity

We have seen earlier how ordinary We have seen earlier how ordinary variables can hold one piece of variables can hold one piece of information information

how arrays can hold a number of pieces of how arrays can hold a number of pieces of information of the same data type information of the same data type

Page 4: C-19

4Rushdi Shams, Dept of CSE, KUET, Bangladesh

What is StructureWhat is Structure

These two data types can handle a These two data types can handle a great variety of situations great variety of situations

But quite often we deal with entities But quite often we deal with entities that are collection of dissimilar data that are collection of dissimilar data types types

Page 5: C-19

5Rushdi Shams, Dept of CSE, KUET, Bangladesh

What is structureWhat is structure

to store data about a book. You to store data about a book. You might want to store might want to store

1.1. its name (a string) its name (a string)

2.2. its price (a float)its price (a float)

3.3. number of pages in it (an int) number of pages in it (an int)

Page 6: C-19

6Rushdi Shams, Dept of CSE, KUET, Bangladesh

What is StructureWhat is Structure

we can follow two approaches we can follow two approaches

1.1. Construct individual arrays, one for Construct individual arrays, one for storing names, another for storing storing names, another for storing prices and still another for storing prices and still another for storing number of pages. number of pages.

2.2. Use a structure variable. Use a structure variable.

Page 7: C-19

7Rushdi Shams, Dept of CSE, KUET, Bangladesh

What is structureWhat is structure

Let us examine these two Let us examine these two approaches one by one approaches one by one

Page 8: C-19

Making things Making things with Arrayswith Arrays

Page 9: C-19

9Rushdi Shams, Dept of CSE, KUET, Bangladesh

Page 10: C-19

Making things Making things with Structureswith Structures

Page 11: C-19

11Rushdi Shams, Dept of CSE, KUET, Bangladesh

Page 12: C-19

Declaration of Declaration of Structure and Structure and

Structure VariablesStructure Variables

Page 13: C-19

13Rushdi Shams, Dept of CSE, KUET, Bangladesh

Page 14: C-19

Initializing Initializing Structure Structure VariablesVariables

Page 15: C-19

15Rushdi Shams, Dept of CSE, KUET, Bangladesh

Page 16: C-19

Accessing Accessing Structure Structure ElementsElements

Page 17: C-19

17Rushdi Shams, Dept of CSE, KUET, Bangladesh

Page 18: C-19

18Rushdi Shams, Dept of CSE, KUET, Bangladesh

Page 19: C-19

Array of Array of StructuresStructures

Page 20: C-19

20Rushdi Shams, Dept of CSE, KUET, Bangladesh