In C++, it allows you to define variables like arrays which allow you to merge many data items of the same kind and structure is another user defined data type that allows you to merge the data items of different kinds.
Structures are used to define a record for example you want to keep track of your employees in an organization.
You may want to track the information of the following attributes about each employee:-
Name
EmpID
Designation
How to define a structure?
While defining a structure you must use the struct statement as it defines a new data type with more than one member.
Let us have a look at the format:
C++
1
2
3
4
5
6
7
8
9
10
11
struct[structuretag]{
member definition;
member definition;
…
member definition;
}
In the above format the structure tag is optional and each member definition is a normal variable definition like int x; float y; or char c; or any other valid variable definition.
At the end of the structure definition before the semi colon you can specify one or more structure variables but that is also optional.
Let us have a look at the example on how to declare the Employee Structure:
C++
1
2
3
4
5
6
7
8
9
structEmployee{
charName[20];
intEmpID;
charDesignation[15];
}emp;
How to access the structure members?
The structure members can be accessed by using the member access operator (.).
The operator is used between the structure variable and the structure member that we want to use.
Let us have a look at the example:
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstring>
usingnamespacestd;
structEmployee{
charName[20];
intEmpID;
charDesignation[15];
};
intmain(){
structEmployee emp1;// Declare emp1 of type Employee
structEmployee emp2;// Declare emp2 of type Employee
The pointer to structures can be defined in the same way as we define the pointer to any other variable.
Let us have a look at the syntax:
C++
1
structstructure_name*struct_pointer;
In the above syntax, you can store the address of a structure variable in the above defined pointer variable and to find the address of a structure variable we will use the “&” operator before the structure name and to access the members of the structure by using the pointer you must use the
Operator like this:
struct_pointer -> Name;
Let us have a look at the example:
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstring>
usingnamespacestd;
voidprintEmp(structEmployee*emp);
structEmployee{
charName[20];
intEmpID;
charDesignation[15];
};
intmain(){
structEmployee emp1;// Declare emp1 of type Employee
structEmployee emp2;// Declare emp2 of type Employee
// employee 1 details
strcpy(emp1.Name,"ABC");
emp1.EmpID=6491;
strcpy(emp1.Designation,"Salesman");
// employee 2 details
strcpy(emp2.Name,"XYZ");
emp2.EmpID=6400;
strcpy(emp2.Designation,"Manager");
// Print emp1 information
printEmp(&emp1 );
// Print emp2 information
printEmp(&emp2 );
return0;
}
// This function accept pointer to structure as parameter.