14 Structures
14 Structures
Chapter-14
STRUCTURES
Introduction:
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:
This structure is named employee. It contains four members: an integer named id no, a
character
array to hold 15 characters for a name, another character array to hold 10 characters for
designation and float named salary.
The structure requires an area in memory, which is 31 bytes long.
The figure given below shows the allocation of memory for the individual elements.
Page 1 of 4
Trinity PU College, Mysuru Computer Science
Example:
Example 1: To access the name of the member emp1 of the tag employee, we write
emp1.name;
Example 2: To access the salary of the member emp2 of the tag employee, we write
emp2.salary
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
};
Page 2 of 4
Trinity PU College, Mysuru Computer Science
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.
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:
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.
Page 3 of 4
Trinity PU College, Mysuru Computer Science
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,
};
Page 4 of 4