c-19
DESCRIPTION
TRANSCRIPT
Lecture 19Lecture 19Version 1.0Version 1.0
StructureStructure
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
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
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
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)
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.
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
Making things Making things with Arrayswith Arrays
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
Making things Making things with Structureswith Structures
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
Declaration of Declaration of Structure and Structure and
Structure VariablesStructure Variables
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
Initializing Initializing Structure Structure VariablesVariables
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
Accessing Accessing Structure Structure ElementsElements
17Rushdi Shams, Dept of CSE, KUET, Bangladesh
18Rushdi Shams, Dept of CSE, KUET, Bangladesh
Array of Array of StructuresStructures
20Rushdi Shams, Dept of CSE, KUET, Bangladesh