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 define C language structure variable

2025-01-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 define the body variable of c language structure". In the daily operation, I believe that many people have doubts about how to define the body variable of c language structure. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to define c language structure variables". Next, please follow the editor to study!

Definition of structural volume variables

The basic definition of structural body variable is similar to that of ordinary variable, but because the structural body type is self-defined in the program, the definition of structural body variable is more flexible and can be used in the following three forms.

1. Define the structure type first and then define the structure variable

If the structure type is already defined in the program, you can define the structure variable in the following form:

Struct structure type name structure variable name

For example, using the student structure type defined earlier to define variables is as follows:

Struct student stud1, stud2

two。 Define the structure variable while defining the structure type.

The general form of this definition of structural body variables is

Struct structure type name

{

Data type member name 1

Data type member name 2

……

Data type member name n

} structure variable name table

For example

Struct student

{

Int id

Char name [20]

Char gender

Double score

} stul, stu2, stu3

Here, while defining the structure type, three structure variables stu1, stu2 and stu3 are defined.

3. Define structure variables directly

This method does not define the name of the structure type, but directly gives the structure type and defines the structure variable at the same time. The general form is

Struct

{

Data type member name 1

Data type member name 2

……

Data type member name n

} structure variable name table

For example:

Struct

{

Int id

Char name [20]

Char gender

Double score

} stu1,stu2,stu3

Note: because this method does not have a type name, the structure type cannot be recorded, so this type can no longer be used to define structure variables elsewhere in the program.

A member variable of a structure type can also be a structure type. For example, the following code defines a structure type date that represents a date type, and when defining a structure type book that represents a book, the member variable publish that represents the publication date is of type struct date.

Struct date

{

Int year

Int month

Int day

}

Struct book

{

Char isbn [15]

Char title [50]

Struct date publish;// uses structure members to represent the publication date

Double price

Int quantity

}

At this point, the study of "how to define the structural variables of c language" 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