C++ Structures store variables having different data types. For instance, n C++ programming language, user sometimes faces a situation where he needs to store a collection of various types of data in a single entity. Arrays provide the functionality to store data having same data types. Whereas for saving data of different data types, Structures are used.
For example, in school management system, the programmer needs to save the details of each student. The student details will include name, grade, address, age etc. For storing these details programmer will create a structure that will contains all the details. The structure can have a name such as student having attributes:
- Name
- Grade
- Age
- Address
Now for all the students, programmer can use the same structure instead of defining a new student with new details.
C++ Structure Declaration and Definition
The syntax for defining structure is:
Here struct is a keyword that is a part of structure declaration. Struct keyword is followed by structure name. Structure name acts as an identifier. The curly braces contain the list of attributes of the structure. Here attributes are named as members. Structure can have multiple attributes. Each attribute can have its own data type(char, int, float, double, string) and name. Each member terminates at a semi colon.
After defining a structure programmer can create objects that will have same members as the structure. For example, below code shows how to define a structure containing student details.
In the above example, the name of the structure is Students and the members of the structure are name, address, class and student_id. The name, address and class members have char data type and their sizes are 50, 50 and 100 respectively. While the student_id member has int data type. Student1 is the object of the structure Students.
Accessing members
C++ allows to access members of a structure using dot operator (.). It is called the member access operator. The syntax for accessing members of structure is:
Below code shows how to access the name member of Students structure:
In this way programmer can assign specific values to members.
The members of structure can not be initialized in structure definition. The member variables are always initialized after there exists an object of the respective structure. The best practice is to define the structures before int main() function. For example, the following code shows the method of using structures and accessing members. The code creates a structure naming location that contains two members. Longitudes and latitudes values.
#include <iostream> using namespace std; struct Location { int Longitude; int Latitude; }; int main() { struct Location site; site.Longitude = 10; site.Latitude = -20; cout << "The location is: "; cout << site.Longitude << ", " << site.Latitude; return 0; }
To declare objects of a certain structure, C++ allows to create multiple objects of a structure by calling struct keyword, followed by structure name, followed by object name. The syntax is:
For example, to create an object of the structure Students, the code is:
Here is another code that creates and prints details of different students.
#include <iostream> #include <iostream> #include <string> using namespace std; struct Students { string name; string grade; string address; int student_id; }; int main() { struct Students Student1; // Declaration of Student 1 struct Students Student2; // Declaration of Student 2 //Student 1 details Student1.name = "Peter Jeff"; Student1.grade = "3b"; Student1.address = "House No.1 Street 6"; Student1.student_id = 0000001; //Student 2 details Student2.name = "Jane Kyle"; Student2.grade = "1a"; Student2.address = "House No. 32 Street 10"; Student2.student_id = 0000002; // Print Student 1 details cout << "The details of student1 are:" << endl; cout << "Name: " << Student1.name <<endl; cout << "Class: " << Student1.grade <<endl; cout << "Address: " << Student1.address <<endl; cout << "Student ID: " << Student1.student_id <<endl; // Print Student 2 details cout << "The details of student2 are:" << endl; cout << "Name: " << Student2.name <<endl; cout << "Class: " << Student2.grade <<endl; cout << "Address: " << Student2.address <<endl; cout << "Student ID: " << Student2.student_id <<endl; return 0; }
The output of above code after compilation is:
Structures as Function Parameters
C++ Functions can take structures as arguments. The method of passing structures to a function as parameters is similar to passing any variable. While declaring or defining a function, the programmer needs to define the structure’s object as function’s parameter. The syntax for passing structure as function parameter is:
Here, void is function’s return type, funcName is the name of function. In parenthesis there is the keyword struct means the parameter is a structure. structure_name is the name of the structure and object is the object of structure.
For example, the code to declare a function that prints details of student is:
Note that the same function should contain the structure definition in order to use structure as function’s parameters. The below code shows a function that takes structure objects as input and print the details:
// This code is written by www.tutuorialsart.com #include <iostream> #include <iostream> #include <string> using namespace std; struct Students { string name; string grade; string address; int student_id; }; void printDetails(struct Students Student) { cout << "The details of Student are:" << endl; cout << "Name: " << Student.name <<endl; cout << "Class: " << Student.grade <<endl; cout << "Address: " << Student.address <<endl; cout << "Student ID: " << Student.student_id <<endl; } int main() { struct Students Student1; Student1.name = "Peter Jeff"; Student1.grade = "3b"; Student1.address = "House No.1 Street 6"; Student1.student_id = 0000001; printDetails(Student1); return 0; }
The output of above code after compilation is:
Members as user input
The members of structure can also take user input values. It is similar to saving user input in variables. For example the below code takes members of the structure as user input:
#include <iostream> #include <iostream> #include <string> using namespace std; struct Students { string name; string grade; string address; int student_id; }; void printDetails(struct Students Student) { cout << "The details of Student are:" << endl; cout << "Name: " << Student.name <<endl; cout << "Class: " << Student.grade <<endl; cout << "Address: " << Student.address <<endl; cout << "Student ID: " << Student.student_id <<endl; } int main() { struct Students Student1; cout << "Enter Student Name: "; getline(cin, Student1.name); cout << "Enter Student Class: "; getline(cin, Student1.grade); cout << "Enter Student Address: "; getline(cin, Student1.address); cout << "Enter Student ID: "; cin >> Student1.student_id; printDetails(Student1); return 0; }
The output of the above code after compilation is:
Pointer to Structures
Like other variables (int, float, char) structures can also have C++ pointers. The syntax for creating a pointer to structure is:
Instead of using dot operator (.) to access structure members, with pointers it is necessary to use arrow operator (->). For example, the below code shows pointer to structure
#include <iostream> using namespace std; struct Location { int Longitude; int Latitude; }; int main() { struct Location loc1 = { 10, -20 }; struct Location* loc2 = &loc1; cout << loc2->Longitude << " " << loc2->Latitude; return 0; }
The output of above code after compilation is:
Advantages of using Structures
Structures provide more code readability and enhance code efficiency.