Chapter 14 Structures1
Chapter 14 Structures1
Chapter-14
STRUCTURES
Introduction:
We have seen variables of simple data types, such as float, char, and int.
Variables of such types represent one item of information: a height, an amount, a count, and so on.
But just as groceries are students into colleges and words into sentences, it is often convenient to
organize simple variables into more complex entities.
The C++ construction called the structure is one way to do this.
A structure is a collection of simple variables. The variables in a structure can be of same or
different types: some can be int, some can be float and so on.
The data items in a structure are called the members of the structure.
For C++ programmers, structures are one of the two important building blocks in the
understanding of objects and classes.
The syntax of a structure is almost identical to that of a class. A structure is a collection of data,
while a class is a collection of both data and function
Defining a structure
The process of defining a structure includes giving the structure a name called the tag and telling
the compiler the name and the data type of each piece of data you want to be included in a
structure.
Syntax:
Struct structure-name
{
datatype member-name-1;
datatype member-name-2;
……………………..
datatype member-name-n;
};
o The keyword struct introduces the structure definition.
o Next comes the structure name or tag, which is part.
o The declarations of the structures members are enclosed in braces.
o The list of all structure members is called template.
o A semicolon follows the closing braces, terminating the entire structure.
1|Page
Chapter 14- Structures I PUC, MDRPUC, Hassan
2|Page
Chapter 14- Structures I PUC, MDRPUC, Hassan
Note:
The definition of a structure does not allocate memory space. The definition just defines the
blueprint for the creation of variables.
A semicolon should follow the last brackets of the structure.
The definition of the structure should be come either before the main function or before any
other variables are declared.
The declaration of the structure variables always comes after the definition. The declaration of
the structure creates space to store the values for the variables.
3|Page
Chapter 14- Structures I PUC, MDRPUC, Hassan
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
void main ( )
{
struct student
{
int regno;
char name[15];
char comb[4];
float perc;
} st;
cout<<”Enter the register number of the student:”;
cin>>st.regno;
cout<<”Enter the name of the student:”;
cin>>st.name;
cout<<”Enter the combination of the student:”;
cin>>st.comb;
cout<<”Enter the percentage of the student:”;
cin>>st.perc;
cout<<”Register No:”<<st.regno<<endl;
cout<<”Name: ”<<st.name<<endl;
cout<<”Combination:”<<st.comb<<endl;
cout<<”Percentage:”<<st.perc<<endl;
getch();
}
Sample run:
Enter the register number of the student: 1234
Enter the name of the student: Nalina M A
Enter the combination of the student: CEBA
Enter the percentage of the student: 90.05
4|Page
Chapter 14- Structures I PUC, MDRPUC, Hassan
Initializing a structure:
Like any other data type, it is also possible to initialize the structure members of a structure
variable.
Structures members are initialized when they are declared.
Example: A program may contain the following initialization of the structure.
employee emp1 = {
1234,
“Lakshmi”,
“Manager”,
10500.00
};
This initialization initializes idno field to 1234, the name field to ”Lakshmi”, the designation field
to “Manager” and the salary field to 10500.00.
Nested structures:
A structure can be defined as a member of another structure. Such structure where one
structure is embedded within another structure is called as a nested structure.
During the declaration of nested structure, the definition of the embedded structure must appear
before the definition of outer structure.
Example: consider the definition of the following structure.
struct distance struct room
{ {
int feet; distance length;
float inches; distance breadth;
}; };
5|Page
Chapter 14- Structures I PUC, MDRPUC, Hassan
The structure room contains another structure distance as one of its members.
Whenever nested structures are used, the definition of the internal structure should precede the
definition of the outer structure.
In the above example, the definition of the structure distance precedes the definition of the
structure room.
Example: consider the definition of structure student.
Array of structures:
A single variable that represents a structure creates memory space to store one set of information.
If we want to create memory space for more instances then we may have to increases the number
of variables or use the facility called arrays.
An array of structure is an array in which each element of the array is a structure. Thus it is a
collection of structures put together as an array.
For example, let us consider that there are 10 students studying in a college. We have defined the
structure so that it could maintain information about students of the college. If we have to store
data of 10 students we need to use arrays rather than a single variable.
When create an array of the structure, we are creating an array of the entire template. This can be
done as follows.
struct student
{
int regno;
char name [15];
char comb;
float fees;
} s [10];
6|Page
Chapter 14- Structures I PUC, MDRPUC, Hassan
In the declaration, s is an array of 10 elements. Each element of the array is a separate structure of
type of student.
The expression:
o s[0].name will access the name of the 1st student, while
o s[9].fees access the fees paid by the last student
regno name comb fees
s[0]
s[1]
……………………….
s[9]
Memory allocation for an array of structures
An array of structures can be assigned initial values just like any other array.
Remember that each element is a structure that must be assigned a corresponding set of initial
values. The process of initialization is illustrated below.
Example:
struct info
{
int regno;
char name[15];
float fees;
};
info s[ ] = {
1, ”Lavanya”, 5500.00,
2, “Manasa”, 6250.00,
3, “Sajay”, 6000.00,
4, “Sahana”, 5900.00,
};
Practical Program 30: Write a program to input the register number, name and class of all the
students in a class into a structure and output the data in a tabular manner with proper
heading.
#include<iostream.h>
#include<conio.h>
struct student
{
int regno;
char name[15];
char section[4];
};
7|Page
Chapter 14- Structures I PUC, MDRPUC, Hassan
void main( )
{
student s[50];
int i, j, n;
clrscr( );
cout<<”How many students?”<<endl;
cin>>n;
for(i=0; i<n; i++)
{
cout<<”Enter the Reg No of the Student ”<<i+1<<”:”;
cin>>s[i].regno;
cout<<”Enter the Name of the Student ”<<i+1<<”:”;
cin>>s[i].name;
cout<<”Enter the Class of the Student ”<<i+1<<”:”;
cin>>s[i].section;
}
cout<<”REG_NO \t NAME \t CLASS \t”<<endl;
for(i=0; i<n; i++)
cout<<s[i].regno<<”\t”<<s[i].name<<”\t”<< s[i].section<<endl;
getch( );
}
**************
8|Page