Lec 03 Structures
Lec 03 Structures
Course Code:CS-1022
Lecture#
Structures
Introduction
• Real world data doesn’t usually deal with information like int,
float etc.
• we have entities that are collections of items, having its own
attributes.
Example:
– a ‘book’ is a collection of attributes such as title, author,
call number, publisher, number of pages, date of
publication, etc.
– All data items are not similar 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 combines different data types that constitutes
an entity.
3
Introduction: Why Use Structures
• If data about say 3 such books is to be stored, then we
can follow two approaches:
– Construct individual arrays, one for storing names,
another for storing prices and still another for storing
number of pages.
– Use a structure variable.
• For the sake of convenience assume that the names of
books would be single character long. Let us begin with
a program that uses arrays.
4
Introduction: Old Approach
void main( )
{
char name[3] ;
float price[3] ;
int pages[3], i ;
cout<< "\nEnter names, prices and no. of pages of 3 books\n" ;
for ( i = 0 ; i <= 2 ; i++ )
cin>>name[i]>>price[i]>>pages[i];
cout<< "\n”<<“Your entered information”<< “\n";
for ( i = 0 ; i <= 2 ; i++ )
cout<< name[i]<<“\n”<<price[i]<<“\n”<<pages[i];
}
5
Introduction:
Output of program
Enter names, prices and no. of pages of 3 books
A 100.00 354
C 256.50 682
F 233.70 512
Your entered information
A 100.000000 354
C 256.500000 682
F 233.700000 512
6
• This approach allows you to store names, prices and
number of pages.
• However, this approach 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.
7
Introduction: 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.
• Structure is a data type whose format is defined by
programmer
• The general form of a structure declaration statement is given
below:
9
Declaring a Structure
Example-1:
struct book
{
char name ;
float price ;
int pages ;
};
This statement defines a new data type called
struct book. // Each variable of this data type will consist of:
•a character variable called name,
•a float variable called price
•an integer variable called pages.
10
Declaring Structure Variables
• Once the new structure data type has been defined one
or more variables can be declared to be of that type.
11
Concepts: Declaration of Structures
• 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. Instead creates a new data type used
to define structure variables
• All structure declaration 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.
12
Accessing Structure Elements
Accessing structure members
•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:
Note that before the dot there must always be a structure variable and
after the dot there must always be a structure element.
13
Initializing Structures
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.
Example
struct book
{
char name[10] ;
float price ;
int pages ;
};
struct book b1 = { "Basic", 130.00, 550 } ;
struct book b2 = { "Physics", 150.80, 800 } ;
14
How Structure Elements are Stored
/* Memory map of structure elements */
struct book
{
char name ;
float price ;
int pages ;
};
main( )
{
struct book b1 = { 'B', 130.00, 550 } ;
Cout<<"\n”<<“Address of name = “<<b1.name ;
Cout<<"\n”<<“Address of price = “<<b1.price ;
Cout<<“\n”<<“Address of pages = “<<b1.pages;
} \\ see output
15
Output
Memory map
16
Additional Features of Structures
• The values of a structure variable can be
assigned to another structure variable of
the same type using the assignment
operator.
17
main( ) {
struct employee
{
char name[10] ;
int age ;
float salary ;
};
struct employee e1 = { " Ahmed ", 30, 15500.50 } ;
struct employee e2, e3 ;
/* piece-meal copying */
strcpy ( e2.name, e1.name ) ;
e2.age = e1.age ;
e2.salary = e1.salary ;
/* copying all elements at one go */
e3 = e2 ;
Cout<< “\n”<<e1.name<<e1.age<<e1.salary ;
Cout<<“\n”<<e2.name<<e2.age<<e2.salary ;
Cout<<“\n”<<e3.name<<e3.age<<e3.salary ;
}
18
Output
• Ahmed 30 15500.500000
• Ahmed 30 15500.500000
• Ahmed 30 15500.500000
19
STRUCTURES AND CLASSES
• Structures are usually used to hold data
only, and classes are used to hold both
data and functions.
• In C++ structures can in fact hold both
data and functions
• Major difference between class and a
structure is that in a class members are
private by default while in a structure they
are public by default.
Summary
• A structure is usually used when we wish to store dissimilar data
together.
21