English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about C ++Structures in programming. What it is, how to define it, and how to use it in a program.
A structure is a collection of variables of different data types under a single name. It is related toClassSimilarly, both store collections of data of different data types.
For example:You want to store some information about a person: their name, ID number, and salary. You can easily create different variables name, citNo, salary to store these pieces of information separately.
But, in the future, you may want to store information about multiple people. Now, you need to create different variables for each piece of information for each person: name1, citNo1, salary1, name2, citNo2, salary2
You can see how large and chaotic the variables and code look. Moreover, since there is no relationship between the variables (information), this will be a daunting task.
A better approach is to collect all relevant information under a Person and use it for each person. Now, the code also looks more concise, readable, and efficient.
All the relevant information collected under a single name Person is a structure.
The struct keyword defines a structure type, followed by an identifier (the name of the structure).
Then, within the curly braces, you can declare one or more members of the structure (declare variables within the curly braces). For example:
struct Person { char name[50]; int age; float salary; };
Here, the structure Person is defined, which has three members: name, age, and salary.
Memory is not allocated when creating a structure.
Structure definition is just a blueprint for creating variables. You can use it as a data type. When you define an integer as follows:
int foo;
int specifies that the variable foo can only accept integer elements. Similarly, the structure definition only specifies the attributes that the structure variable has at the time of definition.
Note: Remember to end with a semicolon();End the declaration of the structure
Once you declare a structure type Person like above, you can define a structure variable as follows:
Person bill;
Here, a structure variable bill is defined, whose type is the structure Person.
After defining the structure variable, the compiler allocates the required memory.
considering that you have32bits or64bit system, float's memory is4bytes, int's memory is4bytes, while the memory of char is1bytes.
Therefore, the structure variable bill has been allocated58bytes of memory.
to usedot (.)operators can access the members of a structure.
Assuming you want to access the member attribute age of the structure variable bill and assign a value to it50. You can use the following code to perform this task:
bill.age = 50;
C ++Program, used to assign data to the members of a structure variable and display it.
#include <iostream> using namespace std; struct Person { char name[50]; int age; float salary; }; int main() { Person p1; cout << "Enter name: "; cin.get(p1.name, 50); cout << "Enter age: "; cin >> p1.age; cout << "Enter salary: "; cin >> p1.salary; cout << "\nDisplay information: " << endl; cout << "Name: " << p1.name << endl; cout << "Age: " << p1.age << endl; cout << "Salary: " << p1.salary; return 0; }
Output Result
Input Name: Zhang Sanxia Input Age: 22 Input Salary: 12200 Display Information: Name: Zhang Sanxia Age: 22 Salary: 12200
Here, declare a structure with three members name, age, and salary.
In main() FunctionInternally, p1Defined a structure variable. Then, prompt the user to input information and display the data entered by the user.
You can also check out these tutorials related to structures: