0% found this document useful (0 votes)
16 views

14 Structures

Uploaded by

Mahika Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

14 Structures

Uploaded by

Mahika Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Trinity PU College, Mysuru Computer Science

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:

The keyword struct introduces the structure definition.


1. Next comes the structure name or tag, which is part.
2. The declarations of the structure’s members are enclosed in braces.
3. The list of all structure members is called template.
4. A semicolon follows the closing braces, terminating the entire structure.

Example: A structure definition to hold employee information.

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

Declaring a structure or defining a structure variable:


A structure can be declared (or a structure variable can be defined) as we defined a basic
built-in data type.
The general form is: structure-name variable;

Example:

Accessing the elements of a structure


After the structure variables are defined, we should know how the member of a structure
variable
is accessed. A member of a structure is always part of a structure and it is not possible to refer
the member directly. The member of a structure can be accessed using dot operator.
The syntax of dot operator is as follows:
structure_variable-name. member_name

The structure member is written in three parts:


1. The name of the structure variable (part1);
2. The dot operator, which consists of a period (.)
3. The member’s name (part3).
Note: The real name of the dot operator is member access operator.

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.

To access the field regno of the structure std, we write std.regno.


Same for (std.doa.dayno , std.dob.year)

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.

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:

Page 3 of 4
Trinity PU College, Mysuru Computer Science

s [0].name will access the name of the 1st student, while


s [9]. fees access the fees paid by the last student.

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

You might also like