C Language Notes: Structures

content

1. The declaration of the structure

1.1 Basic knowledge of structure

1.2 Declaration of structure

1.3 Types of Structure Members

1.4 Definition and initialization of structure variables

2. Access to structure members 


1. The declaration of the 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 variable of a different type. Such as: for people, name, age, gender, etc. are member variables

1.2 Declaration of structure

struct tag //声明一个结构体类型,并不占用空间
{
 member-list;
}variable-list;

Such as: declare a struct type - book:

#include <stdio.h>
#include <string.h >

//声明了一个结构体类型 - 书
/*struct Book
{
	char name[20];//书名
	short price;  //定价
}b4,b5,b6;*/   //b4,b5,b6都是结构体变量 - 全局变量(尽量少使用全局变量)

typedef struct Book
{
	char name[20];//书名
	short price;  //定价
}Book;  //这里的Book是一种类型,而不是变量

int main()
{
	//利用结构体类型创建结构体变量 - 局部变量
	struct Book b1; //创建了一本书
	struct Book b2;
	struct Book b3;
	Book b4;
	Book b5;

	//字符串拷贝
	//把"c语言"拷贝到name数组中。因为不能直接用b.name  = "c语言"来写
	strcpy(b1.name, "c语言"); 
	b1.price = 55;

	printf("%s\n", b1.name);
	printf("%d\n", b1.price);

	return 0;
}

1.3 Types of Structure Members

Members of structures can be scalars, arrays, pointers, or even other structures

struct B
{
	int a;
	char c;
};

struct S
{
	int a; //标量
	char arr[20];//数组
	int* p;//指针
	struct B sb;//结构体
};

1.4 Definition and initialization of structure variables

With the structure type, how to define variables?

struct Point
{
    int x;
    int y;
}p1; //声明类型的同时定义变量p1
struct Point p2; //定义结构体变量p2

//初始化:定义变量的同时赋初值。
struct Point p3 = {x, y};

struct Stu        //类型声明
{
    char name[15];//名字
    int age;      //年龄
};
struct Stu s = {"zhangsan", 20};//初始化

struct Node
{
    int data;
    struct Point p;
    struct Node* next; 
}n1 = {10, {4,5}, NULL}; //结构体嵌套初始化

struct Node n2 = {20, {5, 6}, NULL};//结构体嵌套初始化

2. Access to structure members 

1. Operator ( . ) Structure variable. Member name

2. Operator ( -> ) structure pointer -> member name

void print1(struct Stu stu)
{
	printf("%s %d\n", stu.name, stu.age);
}

void print2(struct Stu* ps)
{
	printf("%s %d\n", (*ps).name, (*ps).age);//正确,但麻烦
	printf("%s %d\n", ps->name, ps->age);//结构体指针->成员
}

int main()
{
	struct Stu s = { "张三", 20 };
	
	print1(s);  //传值调用 stu是s的一份临时拷贝
	print2(&s); //传址调用(优于传值调用)
	return 0;
}

Which of the above print1 and print2 functions is better? Answer: The print2 function is preferred

When passing parameters to a function, the parameters need to be pushed onto the stack. If a structure object is passed, the structure is too large, and the system overhead of parameter stacking is relatively large, so the performance will be degraded.

Explanation of "push stack":

Any function call must apply for space in memory: the application is for stack space

E.g:

int Add(int c, int d)
{
	int z = 0;
	z = c + d;
	return z;
}

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

	return 0;
}

In the system stack:

 

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

Guess you like

Origin blog.csdn.net/m0_62934529/article/details/123553035