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 and use the structure of C language

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

Share

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

This article shows you how to define and use C language constructs, concise and easy to understand, absolutely can make you shine, through the detailed introduction of this article I hope you can gain something.

1.1: What are structures used for?

In life we will encounter a lot of forms, such as your academic report card, there are names, student numbers, subject scores, total scores and so on, these are some different data types, if we want to use these different data in c language how to do? You can use struct variables, which in C is a constructed type of data, as the name suggests, unlike basic atomic variables like int. Think of it as a collection of these fundamental variables.

1.2: Basic definition format of struct variables struct struct name { ; ; ;};

Note:struct is the keyword is the description identifier of the structure type, can not be omitted, the structure name can be omitted, we use typedef struct omitted without writing the structure name

1.3: Definition of struct variables

(We can also declare variables when creating a structure. I won't go into details here. Let me mention my understanding of structure variables: First, a structure is a collection of various basic data types, and on structure variables, we define a collection s={1,2,3,4},s is equivalent to a structure variable. Structure variables contain each member variable in the structure. We can refer to and operate on member variables...)

struct struct name a, b; //defines two struct variables strcut struct name S[4]; //define a struct variable array with four elements strcut struct name * p =&a; //Define a struct pointer that points to the struct variable a1.4 Three references to struct variables. Member name (* struct pointer). Member Name Structure Pointer-> Member Name //This is recommended when using pointers, because in data structures, there are many such references. 2. The use of structure variables (directly using structure variables)#include /* the use of structure */int main(){ struct job_exam //define a job_exam struct { unsigned num; //Student ID char name[10]; //Name char gread_class[10]; //Class int EngLish; //English scores int Chinese; //Language scores int Match; //Math scores int Sum_Exam; //Total score }; struct job_exam Most_Exam_Student; //struct variables are used to store information about the students with the highest scores struct job_exam SomeStudent_news[4] = { {1901,"Longjun,""Class 3-2," 100,100,78},{1902,"Li Hua,""Class 3-2," 99,99,88}, {1903,"Li Ming,""Class 3-2," 99, 98, 90},{1903,"Li Guang,""Class 3-2," 99, 98, 80}; //struct array is used to enter information about 4 classmates for (int i = 0; i

< 4; i++) //依次计算每一位同学的总成绩 { SomeStudent_news[i].Sum_Exam = SomeStudent_news[i].EngLish + SomeStudent_news[i].Chinese + SomeStudent_news[i].Match; } Most_Exam_Student = SomeStudent_news[0]; //假使第一个学生的成绩最高 if (Most_Exam_Student.Sum_Exam < SomeStudent_news[1].Sum_Exam) { Most_Exam_Student = SomeStudent_news[1]; } else if (Most_Exam_Student.Sum_Exam < SomeStudent_news[2].Sum_Exam) { Most_Exam_Student = SomeStudent_news[2]; } else { Most_Exam_Student = SomeStudent_news[3]; } printf("总成绩最高学生的信息为:\n"); printf(" 编号 姓名 班级 英语 语文 数学 总成绩\n"); printf("%6u %7s %7s %6d %6d %6d %6d",Most_Exam_Student.num,Most_Exam_Student.name,Most_Exam_Student.gread_class,Most_Exam_Student.EngLish,Most_Exam_Student.Chinese,Most_Exam_Student.Match,Most_Exam_Student.Sum_Exam);}2.1输出结果

When using structure variables as function parameters for the overall transfer, all members will be transferred one by one, when there are arrays in the members, it will make the transfer time and space costs are very large and seriously reduce the efficiency of the program, so we generally do not directly transfer structure variables when using, but transfer the address of structure variables, reducing the cost of time and space.

3.#include /* struct usage */int Find_Sumexam(struct job_exam* pa); //function declaration struct job_exam //define a global job_exam struct { unsigned num; //Student ID char name[10]; //Name char gread_class[10]; //Class int EngLish; //English scores int Chinese; //Language scores int Match; //Math scores int Sum_Exam; //Total score};struct job_exam SomeStudent_news[4] = { {1901,"Longjun,""Class 3-2," 100,100,78},{1902,"Li Hua,""Class 3-2," 99,99,88}, //struct array is used to enter information about 4 classmates {1903,"Li Ming","Class 3-2",99,98,90},{1903,"Li Guang","Class 3-2",99,98,80} };int main(){ struct job_exam Most_Exam_Student; //struct variables are used to store information about the students with the highest scores for (int i = 0; i

< 4; i++) //依次计算每一位同学的总成绩 { SomeStudent_news[i].Sum_Exam = SomeStudent_news[i].EngLish + SomeStudent_news[i].Chinese + SomeStudent_news[i].Match; } Find_Sumexam(&Most_Exam_Student); //函数调用 printf("总成绩最低学生的信息为:\n"); printf(" 编号 姓名 班级 英语 语文 数学 总成绩\n"); printf("%6u %7s %7s %6d %6d %6d %6d",Most_Exam_Student.num,Most_Exam_Student.name,Most_Exam_Student.gread_class,Most_Exam_Student.EngLish,Most_Exam_Student.Chinese,Most_Exam_Student.Match,Most_Exam_Student.Sum_Exam);}int Find_Sumexam(struct job_exam* pa) //定义一个返回最高学生信息的函数{ *pa = SomeStudent_news[0]; //假使第一个学生的总成绩最高 for (int i = 1; i < 4; i++) { if (pa->

Sum_Exam > SomeStudent_news[i].Sum_Exam) { *pa = SomeStudent_news[i]; //This kind of transformation only needs to find the address of the highest student and return his information. } } return 0;}3.1 Run Results

The above is how to define and use C language constructs. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

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