Concepts and usage of struct in C ++

Source: Internet
Author: User
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;
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, and the access of struct members is carried out through the vertex operator,

Pn1.a = 10 then assign values to 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. To reprint them, be sure to indicate the source and author

# include
# include
usingnamespacestd;

structtest // defines a struct named test
{< br> INTA; // defines struct member A
intb; // define struct member B
};

voidmain ()
{< br> testpn1; // define the struct variable pn1
testpn2; // define the struct variable pn2

pn2.a = 10; // use the member operator. assign a value to Member a in struct variable pn2
pn2. B = 3; // use the member operator. assign a value to Member B in the struct variable pn2

pn1 = pn2; // copy all the Member values of pn2 to the pn1 struct variable with the same structure
cout cout

test * point; // define the structure pointer

point = & pn2; // The Pointer Points to the memory address of the struct variable pn2
cout point-> A = 99; // modify the value of pn2 member A in the structure pointer
cout cout A <"|" B cin. get ();
}

in short, a struct can describe a structure that is not clearly described by an array. It has some features that are not provided by an array.

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.