[C Language Elementary] ❤️ Explore a new data type - structure (must be collected!) ❤️

Kind tips

Hello everyone, I am Cbiltps. In my blog, if there are sentences that are difficult to understand or key points that are difficult to express in words , I will have pictures . So my blog with pictures is very important ! ! !

If you are interested in me please see my first blog !

Key points of this chapter

  • declaration of struct type
  • Structure initialization
  • Struct member access
  • Struct parameter passing

1. Declaration of structure


1.1 Understanding Structures

How did the structure come about?

If a complex object is described, it can be described not only by a variable value (type), but also by a combination of many attributes. At this time, a structure is introduced to express this complex object.

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

insert image description here

1.2 Declaration of structure type (creation)

struct tag
{
    
    
	member_list;
}variable_list;

//sturct 是结构体关键字
//tag 是结构体的名字(标签)
//member_list 是成员变量
//variable_list 是变量列表

//注意:这里的变量列表可以写,也可以不写。
//如果写了:相当于拿前面的结构体类型创建了全局变量

For example to describe a book:

#include <stdio.h>

//结构体类型——因为不是在创建变量,所以不会占用内存
struct Book
{
    
    
	char name[20];//下面的三个属性就是成员变量,每个成员可以是不同类型的
	char author[15];
	float price;
}b1,b2;//b1和b2是全局变量,相当于拿前面结构体类型所创建的变量——放在内存的静态区  
//这里的变量列表也可以省略

int main()
{
    
    
	struct Book b;//这里的b虽然是结构体变量,但是是局部变量——放在内存的栈区

	return 0;
}

There is another way to create struct types:

typedef struct Stu
{
    
    
	char name[20];
	int age;
	char id[10];
}Stu;//在使用typedef的时候,对这个结构体类型重新起名字叫Stu
//注意:在没有typedef的时候,分号前面的是结构体变量。
//        有typedef的时候,分号前面的是结构体类型(重新起名字的)。

int main()
{
    
    
	//那么对这个结构体类型有两种写法:
	struct Stu B;//用的是没有重新起名字的结构体类型
	Stu N;//用的是重新起名字的结构体类型,这种写法更加简洁一些
	
	return 0;
}

insert image description here

1.3 Types of structure members

Members of structures can be variables, arrays, pointers, or even other structures.

1.4 Definition and initialization of structure variables

In the comments of the previous code, we can see that there are actually two definitions of structure variables:

1. Local variables
2. Global variables

insert image description here
So, let's talk about initialization directly:

typedef struct Stu
{
    
    
	char name[20];
	int age;
	char id[10];
}Stu;

int main()
{
    
    
	struct Stu B = {
    
     "张三", 20, "123456" };//就是这样初始化的
	Stu N = {
    
     "李四", 23, "7654323" };

	return 0;
}

Sometimes a structure contains a structure:

struct S
{
    
    
	int a;
	char c;
	double d;
};

struct T
{
    
    
	struct S s;
	char name[20];
	int num;
};

int main()
{
    
    
	struct T t = {
    
     {
    
    100,'c', 3.14}, "lisi", 30 };//结构体中的结构体这样初始化
	return 0;
}

2. Access to structure members


  • Struct Variables Accessing Members
    Struct variable members are accessed through the dot operator ., which accepts two operands.
  • Struct pointer access to members of variables
    Sometimes what we get is not a struct variable, but a pointer to a struct.
struct S
{
    
    
	int a;
	char c;
	double d;
};

struct T
{
    
    
	struct S s;
	char name[20];
	int num;
};

int main()
{
    
    
    //结构体变量访问成员
	struct T t = {
    
     {
    
    100, 'w', 3.14}, "zhangsan", 200 };
	printf("%d %c %f %s %d\n", t.s.a, t.s.c, t.s.d, t.name, t.num);

    //结构体指针访问指向变量的成员
	struct T* pt = &t;
	printf("%d %c %f %s %d\n", pt->s.a, pt->s.c, pt->s.d, pt->name, pt->num);

	return 0;
}

3. Structure parameter passing


#include <stdio.h>

struct S
{
    
    
	int arr[100];
	int num;
	char ch;
	double d;
};

void print1(struct S ss)
{
    
    
	printf("%d %d %d %d %c %lf\n", ss.arr[0], ss.arr[1], ss.arr[2], ss.num, ss.ch, ss.d);
}

int main()
{
    
    
	struct S s = {
    
     {
    
    1,2,3,4,5}, 100, 'w', 3.14};
	print1(s);
	return 0;
}
#include <stdio.h>

struct S
{
    
    
	int arr[100];
	int num;
	char ch;
	double d;
};

void print2(struct S* ps)
{
    
    
	printf("%d %d %d %d %c %lf\n", ps->arr[0], ps->arr[1], ps->arr[2], ps->num, ps->ch, ps->d);
}

int main()
{
    
    
	struct S s = {
    
     {
    
    1,2,3,4,5}, 100, 'w', 3.14 };
	print2(&s);//4个字节
	return 0;
}

Which of the above print1sum print2functions is better?
The answer is: preferred print2functions.

The reason for the preferred print2function:
When a function passes parameters, the parameters need to be pushed onto the stack.
If a structure object is passed, if the structure is too large, the system overhead of pushing the parameters to the stack will be relatively large, which will lead to a
decrease in performance.

Let's draw a picture to understand:
insert image description here
insert image description here

If you don't understand the above stack push , let's explain it with an example:

//以这段代码为例:
#include <stdio.h>
int Add(int x, int y)
{
    
    
	int z = 0;
	z = x + y;
	return z;
}

int main()
{
    
    
	int a = 10;
	int b = 20;
	int c = 0;
	c = Add(a, b);

	return 0;
}

insert image description here

Conclusion: When passing parameters to a structure, it is best to pass the address of the structure.

end of text

Guess you like

Origin blog.csdn.net/Cbiltps/article/details/120319502