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

The use of structures in C language

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

Share

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

This article mainly explains "the use of structures 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 way of thinking to study and learn "the use of structures in C language".

Catalogue

The use of C language structure

1. Declaration and definition of structures

1.1 introduction

1.2 several types defined

two。 Structure initialization

3. Structure member access

3.1 introduction of members

4. Structural parameter transfer

The C language structure uses 1. Declaration and definition of structures 1.1 introduction

We know that an array is a collection of values of the same type, and the structure is that you can put different types of values together.

A structure is a collection of values called member variables. Each member of a structure can be a different type of variable.

The members of a structure can be scalars, arrays, pointers, or even other structures.

For example, we can put some attributes of a student together.

Struct stu// defines a structure type, such as int and char,struct stu are the structure type {/ / member variable char name [20]; / / name int age;// age char id [20]; / / student number}; int main () {struct stu a / / define a variable an of type struct stu, and object a contains several types defined by the attributes of name, age, and student number} 1.2

Type 1:

Struct stu {char name [20]; int age;}; int main () {struct stu a / define the local variable a}

Type 2:

Struct stu {char name [20]; int age;} S1 int age; / define the global variable S1 Magi S22. Structure initialization

The code is as follows:

Struct book {float height; char name [20];} bsetstruct stu {char name [20]; int age; char id [20]; struct book b;}; int main () {/ / initialization is actually the same as array initialization, using curly braces with initialized values. If the structure contains a structure, put a brace inside the curly braces with the initialized values of the internal structure struct stu a = {"bobo", 18, "2021520", {15.8, "free"}};} 3. Structure member access 3.1 member introduction

Members of structural variables are accessed through the dot operator (.). The dot operator accepts two operands, such as: M. n (m is the structure variable and n is the member variable in the structure you want to access)

Pointer access to a structure the member that points to the variable is accessed through the operator (- >). For example, m-> n (m is a pointer to a structure, n is the member variable you want to access in the structure), or (* m) .n

Struct book {float height; char name [20];} bsetstruct stu {char name [20]; int age; char id [20]; struct book b;}; int main () {struct stu a = {"bobo", 18, "2021520", {15.8, "free"}}; / / Franco-printf ("% s\ n", a.name); printf ("% d\ n", a.age) Printf ("% .1f\ n", a.b.height); / / if you access a member of a structure in a structure, access the internal structure first, and then access the member of the internal structure / / struct stu* pa=&a; printf ("% s\ n", pa- > name); printf ("% d\ n", pa- > age); printf (".1f\ n", pa- > b.height) / / method III printf ("% s\ n", (* pa) .name); printf ("% d\ n", (* pa) .age); printf (".1f\ n", (* pa) .b.height);} 4. Structural parameter transfer

Note:

When a structure passes parameters, it is better to pass the address of the structure.

The code is as follows:

Struct book {float height; char name [20];} bsetstruct stu {char name [20]; int age; char id [20]; struct book b;}; void print1 (struct stu p) {printf ("% s% d% s% .1f% s", p.namememery p.ageMed P.b.height.b.name) } void print2 (struct stu* pa) {printf ("% s% d% s% .1f% s", pa- > name,pa- > age,pa- > id,pa- > b.heightpara-> b.name);} int main () {struct stu a = {"bobo", 18, "2021520", {15.8, "free"}}; / / write a function to print the content of a print1 (a) / / the result is: bobo 18 2021520 15.8 free (normal printing) / / write another function to print the content of a print2 (& a) by passing the address of a; / / the result is: bobo 18 2021520 15.8 free (normal printing) return 0;}

So what's the difference between the value and the address, which is better? (preferred address)

When passing parameters, it is actually a copy of the parameters, which will open up a space to store the parameters. Pass the value, is to copy the entire argument to the past, and pass the address, pass only the address, the efficiency of passing parameters is higher.

When passing parameters to a function, the parameters need to be stacked. If you pass a structure object, the structure is too large, the parameter stack of the system overhead is relatively large, so it will lead to performance degradation.

So what is the parameter stack of a function call?

Stack: a data structure that is a special linear table that can only insert and delete at one end

Features: advanced late-out, last-in first-out. (the data entered first is pressed into the bottom of the stack, and the last data is on the top of the stack. When you need to read the data, the data pops up from the top of the stack (the last data is read out first).

Stack: store data in the stack

The call of each function will open up a space on the stack area of memory, and the data will be stored in the stack area one by one, if the data is large and large, the parameter stack will be more difficult, so the performance will be degraded.

Thank you for your reading. the above is the content of "the use of structures in C language". After the study of this article, I believe you have a deeper understanding of the use of structures 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