Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ structure variables

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use C++ structural body variables". In daily operation, I believe many people have doubts about how to use C++ structural body variables. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how to use C++ structural body variables. Next, please follow the editor to study!

The use of structural volume variables

Once the structure type variable is defined, the structure variable can be used in the program for various operations, such as assignment, access, and various operations. Generally speaking, the program can not operate the structural body variables as a whole, but should complete all kinds of operations and operations by referencing the member variables of the structural body variables.

The general form of a member that references a structure variable is:

The name of the structure variable. Member name

Among them, "." Called the member operator. For example:

Struct student

{

Int id

Char name [20]

Char gender

Double score

}

The id member assignment of the struct student stud1; / / structure variable stud1

The id member assignment of the stud1.id=101; / / structure variable stud1

Scanf (% s', & stu.name); / / enter the name member of the structure variable stud1

Scanf ("% lf", & stu.score); / / enter the score member of the structure variable stud1

Putchar (stud1.gender); / / outputs the gender members of the structure variable stud1

Note the following problems when using structural variables and their members

(1) the input and output of structural volume variables cannot be operated as a whole, and the corresponding input and output operations should be carried out on each member of the structural volume variables.

(2) members of structural body variables can perform various operations like ordinary variables of the same type.

(3) if two structure variables are defined using the same structure type, the two variables can be assigned.

For example:

Struct student stu1,stu2

Stu1=stu2

(4) the structural body variable can be initialized while it is defined, and its general form is as follows:

Struct structure type name structure variable name = {initial value of member 1, initial value of member 2,... Member n initial value}

During initialization, the system assigns the initial values of each member in curly braces to the corresponding

Member variable

For example

Struct student

{

Int id

Char name [20]

Char gender

Double score

}

Struct student stu = {101, "zhang", 'masking Magnum 95}

The four member variables of the structural volume variable stu get the corresponding initial values in curly braces in turn. Note that because each member of the structure variable may have a different type, the order of the initial values in the curly braces during initialization is important.

At this point, the study of "how to use C++ structural variables" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report