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 the structure in C language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use the structure in C language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to use the structure in C language.

I. on the structure

In C language, struct refers to a kind of data structure, which is a kind of compound data type (aggregate data type) in C language.

Structures can be declared as variables, pointers, or arrays to implement more complex data structures. A structure is also a collection of elements called members of the structure (member), which can be of different types and are generally accessed by name.

The structure is defined as follows:

Struct tag {member-list} variable-list

Where:

Struct is the structure keyword

Tag is the sign of the structure.

Member-list is a list of structure members, which must list all its members

The variable declared by variable-list for this structure.

In general: tag, member-list, variable-list these three parts should appear at least 2.

II. General definition of structure

Different definitions are applied in different scenarios, so when we program, we need to define the structure according to the actual situation.

Method 1:

Define the structure stu, which is equivalent to a type, such as int. If you want to use this structure, the method is the same as int.

Struct stu {char aa; short bb; int cc;}; struct stu stu1, stu2

Method 2:

Define the structure stu and define the structure variables stu1 and stu2 that need to be used. If you need to define the structure variable later, the method is the same as 1.

Struct stu {char aa; short bb; int cc;} stu1, stu2; struct stu stu3

Method 3:

When you define a structure, the structure name defaults and the structure variable stu1,stu2 is defined. However, structural body variables can no longer be defined later.

Struct {char aa; short bb; int cc;} stu1, stu2

Tip:

Structure variables cannot be defined here as above: (the following error)

Struct stu3; struct stu stu3

Third, use typedef to define structures

Let's briefly talk about typedef here.

In C and C++ programming languages, typedef is a keyword. It is used to give an alias to a data type in order to make the source code easier to read and understand. It is often used to simplify structures that declare complex types, but it is also often seen in integer data types of various lengths, such as size_t and time_t.

Wikipedia

Method 4:

Use typedef to define the structure and alias the stu structure as STU. Instead of using struct stu, you can directly use STU for subsequent definitions.

Typedef struct stu {char aa; short bb; int cc;} STU; STU stu1

You will find that, in fact, it is through the keyword typedef that STU replaces struct stu.

Of course, it can also be used like this:

Struct stu stu1

The above definition loses the meaning of typedef, so it is not recommended.

Method 5:

When using typedef to define a structure, omit the first alias stu of the structure, and add STU directly after it, using the same method as above.

Typedef struct {char aa; short bb; int cc;} STU; STU stu1

Method 6:

There is also a way of definition that conforms to grammatical rules but does not make much sense.

Typedef struct stu {char aa; short bb; int cc;}; struct stu stu1

The above definitions can be regarded as the knowledge of grammar. If you don't understand it, please review it again.

IV. Calculation of the size of the structure

I believe many people do not understand the size of the structure. In actual programming, it is also used in many places, such as: storing and copying structures will involve the size of structures.

1. Compare the size of the two structures, is it the same?

Structure 1:

Struct stu {char aa; short bb; char cc;} stu1

Structure 2:

Struct stu {char aa; char bb; short cc;} stu2

The answer is different. The program tests show that sizeof (stu1) = 6, sizeof (stu2) = 4.

two。 Analyze the different reasons

The calculation of structure should follow the principle of byte alignment, which generally meets three criteria:

The first address of a structure variable can be divisible by the size of its widest base type member.

The offset of each member of the structure from the first address of the structure is an integral multiple of the size of the member. If necessary, the compiler will add padding bytes between the members.

The total size of the structure is an integral multiple of the widest base type member size of the structure, and the compiler will add padding bytes after the last member if necessary

For the above struct stu1, the maximum byte is 2 bytes, in the order char- > short- > char:

For the above struct stu2, the maximum byte is 2 bytes in the order of char- > char- > short:

Through the above two tables, I believe you should understand, changed to 4 bytes of int, the principle is the same.

Thank you for your reading. the above is the content of "how to use the structure in C language". After the study of this article, I believe you have a deeper understanding of how to use the structure in C language. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report