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

What is 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 is to share with you about what the structure is in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

one。 Structure definition

C language structure is a collection of the same or different types of data, and the structure type is the data type defined by the struct keyword.

The format of the structure is as follows:

Struct structure name {data members contained in the structure, including arrays of variables} structure variables; / / structure variables can specify one or more

For example:

Struct Student {char name [10]; char sex [2]; int age;} Stu II. Example demonstration

First, let's take a look at how to initialize the structure body variable.

# include # include struct Student {char name [10]; char sex [5]; int age;} Stu = {"Mike", "man", 22}; int main (int argc, char * argv []) {printf ("name:%s\ nsex:%s\ nage:%d\ n", Stu.name,Stu.sex,Stu.age);}

Initializing the structure variable is simple and assigns values directly after the structure variable.

Results:

Structure as function parameter # include # include / / define Student structure struct Student {char name [10]; char sex [5]; int age;} Stu;void print (struct Student stu) {printf ("Student name:%s\ n", stu.name); printf ("Student sex:%s\ n", stu.sex); printf ("Student age:%d\ n", stu.age) } int main (int argc, char * argv []) {struct Student stu1; strcpy (stu1.name, "will"); strcpy (stu1.sex, "man"); stu1.age = 20; print (stu1); / / Stu Stu.age=11; print (Stu);}

From this example, we can see that when we pass the structure as a parameter to the function, when we define the structure, we can define the structure variable before, so there is no need to define the structure variable, for example: struct Student stu1; assumes that stu1 defines the variable when defining the structure, then we can assign values directly.

Results:

You can see that the second student prints, because the structure variable is already defined when the structure is defined, so it can be assigned directly.

Structure pointer

Example demonstration, passing in structure pointer

# include # include struct Student {char name [10]; char sex [5]; int age;} Stu;void print (struct Student * stu) {printf ("Student name:%s\ n", stu- > name); printf ("Student sex:%s\ n", stu- > sex); printf ("Student age:%d\ n", stu- > age);} int main (int argc, char * argv []) {struct Student stu1 Strcpy (stu1.name, "will"); strcpy (stu1.sex, "man"); stu1.age = 20; print (& stu1); Stu.age=11; print (& Stu);}

The main difference between the example here and the above example is:

1. Change the defined variable to the pointer struct Student * stu.

two。 Use-> when assigning pointers.

3. When using the print function, take the address instead.

The results are consistent.

III. The difference between typedef struct and struct 1. Different statements

1), struct:struct can declare the structure directly using the structure name.

2) typedef struct:typedef struct is a modified structure, and the structure has an alias, and the structure is declared through the structure alias.

2. Access member variables are different

1), the structure body variable defined by struct:struct, which can access the structure member directly.

2), the structure body variable defined by typedef struct:typedef struct can not access the structure member directly, but must access the member explicitly through the structure volume variable.

3. Redefine different

1), struct: if you want to redefine the struct structure, you must rewrite the entire structure.

2), typedef struct: if you want to redefine the typedef struct structure, you can redefine it by inheriting the structure through aliases.

For example:

You can see:

Using structures defined by typedef struct, we usually operate with aliases and simplify the use of them, such as Stu S1, which is the same as declaring objects. If we use struct, we should write it as struct Student stu1;.

If you use the structure name directly, an error will be reported:

Example of error:

Error result:

Change back to the alias operation, and the result:

Thank you for reading! This is the end of the article on "what is the structure in C language". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

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