Struct(Struct) is a collection of data with the same type or different types of data. It is also called a structure. It is recommended that you combine the structure and enumeration in C ++ with this article.
What is a struct?
Simply put, a struct is a structure that can contain different data types. It is a data type that can be defined by itself. Its features are different from those of arrays, first, struct can declare different data types in a structure. Second, struct variables with the same structure can be assigned values to each other, but arrays cannot, because an array is a data set of a single data type, it is not a data type (while a struct is), and the array name is a constant pointer, it cannot be used as a left value for computation, therefore, arrays cannot be duplicated by array names, even if the data type and array size are identical.
Define the struct using the struct modifier, for example:
C ++ code:
- struct test
- {
- float a;
- int b;
- };
The code above defines a struct named test. Its data type is test. It contains two members, a and B. The data type of member a is floating point, the data type of member B is integer.
Since struct itself is a custom data type, the method for defining struct variables is the same as that for defining common variables.
- test pn1;
In this way, the pn1 struct variable of the test struct data type is defined. The access of struct members is carried out through the vertex operator. If pn1.a = 10, a value assignment is performed on member a of the struct variable pn1,
Note: The structure itself does not occupy any memory space during its life. The computer allocates memory only when you define the struct variable with the struct type you defined.
Struct can also define pointers. struct pointers are called structure pointers.
The structure pointer uses the-> symbol to access members. The following is a complete example of the above description:
C ++ code:
- // Program Author: Guan Ning
- // All manuscripts are copyrighted. If you want to reprint them, be sure to indicate the source and author.
- # Include <iostream>
- # Include <string>
- Usingnamespacestd;
- Structtest // define a struct named test
- {
- Inta; // defines struct member.
- Intb; // define struct member B
- };
-
- Voidmain ()
- {
- Testpn1; // define the struct variable pn1
- Testpn2; // define the struct variable pn2
-
- Pn2.a = 10; // assign a value to Member a in struct variable pn2 through the member operator.
- Pn2. B = 3; // assign a value to Member B in struct variable pn2 through the member operator.
-
- Pn1 = pn2; // copy all the Member values in pn2 to the struct variable pn1 with the same structure
- Cout <pn1.a <"|" <pn1. B <endl;
- Cout <pn2.a <"|" <pn2. B <endl;
-
- Test * point; // define the structure pointer
-
- Point = & pn2; // pointer to the memory address of the pn2 struct variable
- Cout <pn2.a <"|" <pn2. B <endl;
- Point-> a = 99; // use the structure pointer to modify the value of pn2 member.
- Cout <pn2.a <"|" <pn2. B <endl;
- Cout <point-> a <"|" <point-> B <endl;
- Cin. get ();
- }
In short, struct can describe the structure that cannot be clearly described by arrays. It has some features that arrays do not possess.