Now, you'd need to create different variables for each information per person:
name1,
citNo1, salary1, name2, citNo2, salary2
You can easily visualize how big and messy the code would look. Also, since no relation
between the variables (information) would exist, it's going to be a daunting task.
A better approach will be to have a collection of all related information under a single
name
Person, and use it for every person. Now, the code looks much cleaner, readable
and efficient as well.
This collection of all related information under a single name
Person is a structure.
Declaration of a Structure in C++
Then inside the curly braces, you can declare one or more members (declare variables
inside curly braces) of that structure. For example:
struct
Person
{
char name[50];
int age;
float salary;
};
Here a structure
person is defined which has three members:
name,
age and
salary.
When a structure is created, no memory is allocated. The structure definition is only the
blueprint for the creating of variables. You can imagine it as a datatype. When you define
an integer as below:
int foo;
The
int specifies that, variable
foo can hold integer element only. Similarly, structure
definition only specifies that, what property a structure variable holds when it is defined.
Defining Variable of a Structure
Once you declare a structure
person as above. You can define a structure variable as:

Person bill;
Here, a structure variable
bill is defined which is of type structure P
erson.
When structure variable is defined, only then the required memory is allocated by the
compiler.
Considering you have either 32-bit or 64-bit system, the memory of
float is 4 bytes,
memory of
int is 4 bytes and memory of
char is 1 byte.
Hence, 58 bytes of memory is allocated for structure variable
bill.
Accessing members of a Structure
The members of structure variable is accessed using a
dot (.)
operator.
Suppose, you want to access
age of structure variable
bill and assign it 50 to it. You can
perform this task by using following code below:
bill.age = 50;
Example:
#include
<iostream>
using namespace
std;
struct
Person
{
char
name[
50
];
int
age;
float
salary;
};
int
main()
{
Person
p1;
cout <<
"Enter Full name: "
;
cin.
get
(p1.name,
50
);
cout <<
"Enter age: "
;
cin >> p1.age;
cout <<
"Enter salary: "
;
cin >> p1.salary;
cout <<
"\nDisplaying Information."
<< endl;
cout <<
"Name: "
<< p1.name << endl;
cout <<
"Age: "
<< p1.age << endl;
cout <<
"Salary: "
<< p1.salary;
return
0
;
}

Output
Enter Full name: Magdalena Dankova
Enter age: 27
Enter salary: 1024.4
Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4
Structure and Functions
Structure variables can be passed to a function and returned in a similar way as normal
arguments.
Example:
#include
<iostream>
using namespace
std;
struct
Person
{
char
name[
50
];
int
age;
float
salary;
};
void
displayData(
Person
);
// Function declaration
int
main()
{
Person
p;
cout <<
"Enter Full name: "
;
cin.
get
(p.name,
50
);
cout <<
"Enter age: "
;
cin >> p.age;
cout <<
"Enter salary: "
;
cin >> p.salary;
// Function call with structure variable as an argument
displayData(p);
return
0
;
}

void
displayData(
Person
p)
{
cout <<
"\nDisplaying Information."
<< endl;
cout <<
"Name: "
<< p.name << endl;
cout <<
"Age: "
<< p.age << endl;
cout <<
"Salary: "
<< p.salary;
}
Output


You've reached the end of your free preview.
Want to read all 75 pages?
- Fall '19
- Bilal Shahid
- Control flow, do-while loop