PREV NEXT
What are data structures?
- 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:
1 2 3 4 5 6 7 8 9 10 11 |
struct[structure tag] { 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:
1 2 3 4 5 6 7 8 9 |
struct Employee { char Name[20]; int EmpID; char Designation[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:
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> using namespace std; struct Employee { char Name[20]; int EmpID; char Designation[15]; }; int main() { struct Employee emp1; // Declare emp1 of type Employee struct Employee 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 Employee1 information cout << "Employee 1 Name : " << emp1.Name <<endl; cout << "Employee 1 EmpID : " << emp1.EmpID <<endl; cout << "Employee 1 Designation : " << emp1.Designation <<endl; // Print Employee2 information cout << "Employee 2 Name : " << emp2.Name <<endl; cout << "Employee 2 EmpID : " << emp2.EmpID <<endl; cout << "Employee 2 Designation : " << emp2.Designation <<endl; return 0; } |
What are Structures as Function Arguments?
The structures can be passing as function arguments in the same way as you pass any other variable or pointer.
You would access the structure variables in the same way as we have accessed the structures in the above example:
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 |
#include <iostream> #include <cstring> using namespace std; void printEmp( struct Employee emp ); struct Employee { char Name[20]; int EmpID; char Designation[15]; }; int main() { struct Employee emp1; // Declare emp1 of type Employee struct Employee 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 ); return 0; } void printEmp( struct Employee emp ) { cout << "Employee Name : " << emp.Name <<endl; cout << "Employee Empid : " << emp.EmpID <<endl; cout << "Employee Designation : " << emp.Designation <<endl; } |
What are Pointers to Structures?
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:
1 |
struct structure_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:
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> using namespace std; void printEmp( struct Employee *emp ); struct Employee { char Name[20]; int EmpID; char Designation[15]; }; int main() { struct Employee emp1; // Declare emp1 of type Employee struct Employee 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 ); return 0; } // This function accept pointer to structure as parameter. void printEmp( struct Employee *emp ) { cout << "Employee Name : " << emp->Name <<endl; cout << "Employee EmpID : " << emp->EmpID <<endl; cout << "Employee Designation : " << emp->Designation <<endl; } |