Lecture 19
C language wouldn’t have been so popular had it been able to
handle only all ints, or all floats or all chars at a
time. In fact when we handle real world data, we don’t usually deal with little
atoms of information by themselves—things like integers, characters and such.
Instead we deal with entities that are collections of things, each thing having
its own attributes, just as the entity we call a ‘book’ is a collection of
things such as title, author, call number, publisher, number of pages, date of
publication, etc. As you can see all this data is dissimilar, for example
author is a string, whereas number of pages is an integer. For dealing with
such collections, C provides a data type called ‘structure’. A structure gathers
together, different atoms of information that comprise a given entity.
We have seen earlier how ordinary
variables can hold one piece of information and how arrays can hold a number of
pieces of information of the same data type. These two data types can handle a
great variety of situations. But quite often we deal with entities that are
collection of dissimilar data types.
For example, suppose you want to store
data about a book. You might want to store its name (a string), its price (a
float) and number of pages in it (an int). If data about say 3 such books is to
be stored, then we can follow two approaches:
1. Construct
individual arrays, one for storing names, another for storing prices and still
another for storing number of pages.
2. Use
a structure variable.
Let us examine these two approaches one by
one. For the sake of programming convenience assume that the names of books would
be single character long. Let us begin with a program that uses arrays.
This
approach no doubt allows you to store names, prices and number of pages. But as
you must have realized, it is an unwieldy approach that obscures the fact that
you are dealing with a group of characteristics related to a single entity—the
book.
The program
becomes more difficult to handle as the number of items relating to the book go
on increasing. For example, we would be required to use a number of arrays, if
we also decide to store name of the publisher, date of purchase of book, etc.
To solve this problem, C provides a special data type—the structure.
A structure
contains a number of data types grouped together. These data types may or may
not be of the same type. The following example illustrates the use of this data
type.
This
program demonstrates two fundamental aspects of structures:
(a)
declaration of a structure
(b)
accessing of structure elements
By the way,
Like
primary variables and arrays, structure variables can also be initialized where
they are declared. The format used is quite similar to that used to initiate arrays.
Note the
following points while declaring a structure type:
The closing
brace in the structure type declaration must be followed by a semicolon.
It is
important to understand that a structure type declaration does not tell the
compiler to reserve any space in memory. All a structure declaration does is,
it defines the ‘form’ of the structure.
Usually
structure type declaration appears at the top of the source code file, before
any variables or functions are defined. In very large programs they are usually
put in a separate header file, and the file is included (using the preprocessor
directive #include) in whichever program we want to use this structure type.
Accessing
Structure Elements
Having
declared the structure type and the structure variables, let us see how the
elements of the structure can be accessed.
In arrays
we can access individual elements of an array using a subscript. Structures use
a different scheme. They use a dot (.) operator. So to refer to pages of
the structure defined in our sample program we have to use,
b1.pages
Similarly,
to refer to price we would use,
b1.price
Note that
before the dot there must always be a structure variable and after the dot
there must always be a structure element.
How
Structure Elements are Stored
Whatever be
the elements of a structure, they are always stored in contiguous memory
locations. The following program would illustrate this:
Actually
the structure elements are stored in memory as shown
Array of Structures
Our sample program showing usage of structure
is rather simple minded. All it does is, it receives values into various
structure elements and output these values. But that’s all we intended to do
anyway... show how structure types are created, how structure variables are
declared and how individual elements of a structure variable are referenced.
In our sample program, to store data of 100
books we would be required to use 100 different structure variables from b1 to
b100, which is definitely not very convenient. A better approach would
be to use an array of structures. Following program shows how to use an array
of structures.
1 comments:
This is great blog! Keep it up. chemical overhaul
Post a Comment