[C language] primary structure

About the author: Hello, everyone. I'm rage Yu Tao Xia
🦸 Personal homepage: Berserker Yu Tao Xia
Support me: focus on not getting lost and improve mutual intimacy 💌💌💌
This time I learned a little about the MD compiler, so I came to write a blog to try
Bye, say rush!!!

🐹 1. Declaration of structure

🌷 1.1 basic knowledge of structure

🦋 A structure is a collection of values called member variables. Each member of the structure can be a different type of variable.
Why should there be a structure? Think like this. If you want to represent a number, it's easy to use int, then if it's a 🐶 And? That's why there are structures. We can define the kind of dog, gender, coat color, name, etc. and attribute them to one structure 🐶.

🌷 1.2 declaration of structure

```//The essence is to create an object
struct tag//Variable name
{
member-list;
}variable-list;//Variable list is a global variable

```

🌷 1.3 types of structure members

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

🌷 1.4 definition and initialization of structure variables

With the structure type, how to define variables is actually very simple.

```struct Point
{
int x;
int y;
}p1; //Define the variable p1 while declaring the type

struct Point p2; //Define structure variable p2

struct Point p3 = {x, y};

struct Stu        //Type declaration
{
char name[15];//name
int age;      //Age
};

struct Stu s = {"zhangsan", 20};//initialization

struct Node
{
int data;
struct Point p;
struct Node* next;
}n1 = {10, {4,5}, NULL}; //Structure nesting initialization

struct Node n2 = {20, {5, 6}, NULL};//Structure nesting initialization
```

🐹 2. Visits of structural members

Structure variable access member
The members of the structure variable are passed through the point operator (.) Visited. The point operator accepts two operands.
Struct pointers access members that point to variables
Sometimes we get a pointer to a structure instead of a structure.

```struct B
{
char c;
short s;
double d;
};

struct Stu
{
//Member variable
struct B sb;//Note include
char name[20];//name
int age;//Age
char id[20];
} s1,s2;//s1 and s2 are also structural variables, and s1 and s2 are global variables

int main()
{
//s is a local variable
struct Stu s = { {'w', 20, 3.14}, "Zhang San", 30, "202005034"};//object
//.  ->
printf("%c\n", s.sb.c);//w
printf("%s\n", s.id);//202005034

struct Stu* ps = &s;
printf("%c\n", (*ps).sb.c);//w
printf("%c\n", ps->sb.c);//w
}
```

🐹 3. Structural transmission parameters

Direct code:

```struct B
{
char c;
short s;
double d;
};

struct Stu
{
//Member variable
struct B sb;//Note include
char name[20];//name
int age;//Age
char id[20];
} s1,s2;//s1 and s2 are also structural variables, and s1 and s2 are global variables

void print1(struct Stu t)
{
printf("%c %d %lf %s %d %s\n", t.sb.c, t.sb.s, t.sb.d, t.name, t.age, t.id);
}

void print2(struct Stu* ps)
{
printf("%c %d %lf %s %d %s\n", ps->sb.c, ps->sb.s, ps->sb.d, ps->name, ps->age, ps->id);
}
int main()
{
//s is a local variable
struct Stu s = { {'w', 20, 3.14}, "Zhang San", 30, "202005034"};//object
print1(s);//Value transfer call / / w 20 3.14 Zhang San 30 202005034
print2(&s); //Address transfer call / / w 20 3.14 Zhang San 30 202005034
return 0;
}
```

Which of the print1 and print2 functions above is better?

 When we write programs normally, we use multiple address calls, which can save more memory and faster than value calls

When a function passes parameters, the parameters need to be pressed on the stack. If the structure is too large when passing a structure object, the system overhead of parameter stack pressing is relatively large, which will lead to performance degradation.

Conclusion:
When the structure passes parameters, the address of the structure should be passed.

🥶 Simple understanding of parameter stack pressing

Can you understand what I want to express?

Tags: C

Posted by zeddeh on Sat, 16 Apr 2022 00:40:00 +0930