Concepts and usage of struct in C ++

Source: Internet
Author: User

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:

 
 
  1. struct test   
  2. {   
  3.  float a;   
  4.  int b;   
  5. };  

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.

 
 
  1. 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:

 
 
  1. // Program Author: Guan Ning
  2. // All manuscripts are copyrighted. If you want to reprint them, be sure to indicate the source and author.
  3. # Include <iostream>
  4. # Include <string>
  5. Usingnamespacestd;
  6. Structtest // define a struct named test
  7. {
  8. Inta; // defines struct member.
  9. Intb; // define struct member B
  10. };
  11.  
  12. Voidmain ()
  13. {
  14. Testpn1; // define the struct variable pn1
  15. Testpn2; // define the struct variable pn2
  16.  
  17. Pn2.a = 10; // assign a value to Member a in struct variable pn2 through the member operator.
  18. Pn2. B = 3; // assign a value to Member B in struct variable pn2 through the member operator.
  19.  
  20. Pn1 = pn2; // copy all the Member values in pn2 to the struct variable pn1 with the same structure
  21. Cout <pn1.a <"|" <pn1. B <endl;
  22. Cout <pn2.a <"|" <pn2. B <endl;
  23.  
  24. Test * point; // define the structure pointer
  25.  
  26. Point = & pn2; // pointer to the memory address of the pn2 struct variable
  27. Cout <pn2.a <"|" <pn2. B <endl;
  28. Point-> a = 99; // use the structure pointer to modify the value of pn2 member.
  29. Cout <pn2.a <"|" <pn2. B <endl;
  30. Cout <point-> a <"|" <point-> B <endl;
  31. Cin. get ();
  32. }

In short, struct can describe the structure that cannot be clearly described by arrays. It has some features that arrays do not possess.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.