The document discusses structures in C programming. It explains that structures allow storing different data types together, like storing the name, price, and number of pages for a book. Arrays can only store the same data type, so multiple arrays would be needed. Structures provide a better approach by allowing dissimilar data types to be grouped together as a single unit called a structure variable. The document covers declaring and initializing structure variables, as well as accessing individual elements within a structure. It also discusses creating arrays of structures.
2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StructureWhat is Structure
a ‘book’ is a collection of things such as title,a ‘book’ is a collection of things such as title,
author, call number, publisher, number of pages,author, call number, publisher, number of pages,
date of publication, etcdate of publication, etc
all this data is dissimilarall this data is dissimilar
author is a string, whereas number of pages is anauthor is a string, whereas number of pages is an
integerinteger
3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StructureWhat is Structure
For dealing with such collections, C provides aFor dealing with such collections, C provides a
data type called ‘structure’data type called ‘structure’
A structure gathers together, different atoms ofA structure gathers together, different atoms of
information that comprise a given entityinformation that comprise a given entity
We have seen earlier how ordinary variables canWe have seen earlier how ordinary variables can
hold one piece of informationhold one piece of information
how arrays can hold a number of pieces ofhow arrays can hold a number of pieces of
information of the same data typeinformation of the same data type
4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StructureWhat is Structure
These two data types can handle a great varietyThese two data types can handle a great variety
of situationsof situations
But quite often we deal with entities that areBut quite often we deal with entities that are
collection of dissimilar data typescollection of dissimilar data types
5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is structureWhat is structure
to store data about a book. You might want toto store data about a book. You might want to
storestore
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)
6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StructureWhat is Structure
we can follow two approacheswe can follow two approaches
1.1. Construct individual arrays, one for storingConstruct individual arrays, one for storing
names, another for storing prices and stillnames, another for storing prices and still
another for storing number of pages.another for storing number of pages.
2.2. Use a structure variable.Use a structure variable.
7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is structureWhat is structure
Let us examine these two approaches one byLet us examine these two approaches one by
oneone