In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "C language structure Application case Analysis". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this article "C language structure Application case Analysis" can help you solve the problem.
1. Declaration of the structure 1.1 basic knowledge of the structure
A structure is a collection of values called member variables. Each member of a structure can be a different type of variable
1.2 declaration of structure struct tag {member-list;} variable-list
The following ways are the same, defining the structural type of the student, including: name, age, gender, student number
/ / for example, 1struct Stu {char name [20]; / / name int age;//, age char sex [5]; / / gender char id [20]; / / student number} / / semicolons cannot be lost / / for example, 2struct Stu {char name [20]; / / name int age;// age char sex [5]; / / gender char id [20]; / / student ID} S1 _ mens _ 2 _ 3 / / variables of three structure types are defined / / example 3typedef struct stu stu;// example 4typedef struct stu {char name [20]; int age; char sex [8]; float score;} stu;int main () {struct stu stu4;// local variable struct stu stu5; stu stu6;// global variable return 0;} 1.3 Type of structure member
Members of a structure can be scalars, arrays, pointers, or even other structures
1.4 definition and initialization of structure variables struct Point {int x; int y;} p1; / / definition of variable p1struct Point p2 while declaring types; / / definition of structure variables p2ax / initialization: initial values are assigned while defining variables. Struct Point p3 = {x, y}; struct Stu / / Type declaration {char name [15]; / / name int age; / / Age}; struct Stu s = {"zhangsan", 20}; / / initialize struct Node {int data; struct Point p; struct Node* next;} N1 = {10, {4 min5}, NULL} / / structure nesting initialization struct Node N2 = {20, {5, 6}, NULL}; / / structure nesting initialization 2, access to structure members 2.1 dot operator access
Members of structural variables are accessed through the dot operator (.). The dot operator accepts two operands: the variable name. Members
Struct S Sitstrcpy (s.name, "zhangsan"); / / use. Visit the name member s.age = 20 Splash / use. Access age member 2.2-> operator access
A pointer to a structure that accesses a member pointing to a variable, variable name-> member
Struct Stu {char name [20]; int age;}; void print (struct Stu* ps) {printf ("name =% s age =% d\ n", (* ps) .name, (* ps) .age); / / use the structure pointer to access the member printf pointing to the object ("name =% s age =% d\ n", ps- > name, ps- > age) } 3. Structure passing parameters 3.1 parameters are variables of structure type struct point {int x; int y;} p1 = {10jue 20}; struct sdeband / structure {char c; struct point sp; double d; char arr [20]; / / string}; void print1 (struct s ss) / / pass parameter structure {printf ("% c\ n", ss.c) Printf ("% d\ n", ss.sp.x); printf ("% d\ n", ss.sp.y); printf ("% lf\ n", ss.d); printf ("% s\ n", ss.arr);} int main () {struct point p1 = {100,200} / / define a variable and initialize struct s ss = {'wicked, {100jigree 20}, 5.5, "hello"}; / / initialize ss.c =' baked; ss.sp.x = 1000; ss.sp.y = 2000; ss.d = 3.14; / / ss.arr = "wolrd"; wrong method strcpy (ss.arr, "world") / / string assignment function print1 (ss); / / print the address of the structure variable} 3.2.The parameter is a variable of the structure type struct point {int x; int y;} p1 = {10 int x; int y; 20}; struct s {char c; struct point sp; double d; char arr [20]; / / string} Void print2 (struct s* ss) / / pass parameter address {printf ("% c\ n", ss- > c); printf ("% d\ n", ss- > sp.x); printf ("% d\ n", ss- > sp.y); printf ("% lf\ n", ss- > d); printf ("% s\ n", ss- > arr) } int main () {struct s ss = {'walled, {100jing20}, 5.5, "hello"}; / / initialize ss.c = 'baked; ss.sp.x = 1000; ss.sp.y = 2000; ss.d = 3.14; / / ss.arr = "wolrd"; wrong method strcpy (ss.arr, "world"); print2 (& ss) / / print structure volume variable} 3.3 structure transmission parameter comparison
The effect of the above two functions is the same, as shown in the following figure:
The function print1 accepts structural type variables, which are formal parameters. In addition, it opens up space and copies the arguments, which takes up a lot of memory space.
-the function print2 accepts the address of the structure type, defines the pointer receiving address of the structure type, only opens up the storage address, and takes up a small amount of memory space.
* * Note: * * 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. Therefore, when the structure passes parameters, it is necessary to pass the address of the structure, that is, the function print2 is better than the function print1.
This is the end of the content of "C language structure Application example Analysis". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.