[Must-see for entry to C language] Structure

Structure
This chapter has a simple understanding of structure, and will focus on this part in the advanced custom type later.

First, the declaration of the structure type

Structure: used to describe complex objects with more than one property

A structure is a collection of values ​​that become member variables, and the members of the structure can be variables of different types.

We have already learned: int, char, float, doubleand other single variable types, but these can only describe a single attribute, so the C language creates a structure containing multiple single variables to facilitate the description of complex objects. For example, a book has attributes such as title, author, and price.
Declare syntax for structs:

	struct tag//struct - 结构体关键字;tag - 结构体标签;struct tag - 结构体类型名
	{
    
    
		member-list;//成员列表
	}variable-list;//变量列表

int类型It does not occupy space by itself, and only opens up space in memory when it is int类型created . In the same way, it does not occupy space by itself, and it will open up space in memory when it is created .int整型变量
struct tag结构体类型结构体类型结构体变量

To declare a student type, its member variables include: name, age, gender, student number.

struct Stu
{
    
    
  //成员变量
 char name[20];//名字
 int age;//年龄
 char sex[5];//性别
 char id[20];//学号
}

When you feel that the above struct Stustructure type name is too long, you need to use type redefinition at this time.typedef

typedef struct Stu
{
    
    
  //成员变量
 char name[20];//名字
 int age;//年龄
 char sex[5];//性别
 char id[20];//学号
}Stu;

Thus, struct Stuit is equivalent toStu

Types of
struct members: The members of a struct can be scalars, arrays, pointers, or even other structs (inline).

Second, the structure initialization

With struct types, defining struct variables is easy!

struct Stu
{
    
    
	//成员变量
	char name[20];//名字
	int age;//年龄
	char sex[5];//性别
	char id[20];//学号
}s1;//①声明类型的同时定义变量s1
struct Stu s2;//定义全局变量s2
int main()
{
    
    
	struct Stu s3;//③定义局部变量s3
	struct Stu s4 = {
    
     "张三",17,"男","20200360318" };//④定义结构体变量s4的同时可初始化
	return 0;
}

The above is how to create a structure variable. can be replaced with typedef struct {...} Stu;in the code after performing type redefinition .struct StuStu

Note: mainThe structure variable created in the function is a local variable, which opens up space in the stack area; mainoutside the function, it is a global variable and opens up space in the static area.

Structures can be nested within structures:

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}, "张三", 17 };
	return 0;
}

The structure variable t contains useful 结构体类型Screation s. It is used when sinitializing {}.

Three, structure member access

  1. .Operator
    Structure variable members can be accessed .through the operator, which has two operands, the left side is the structure variable name, and the right side is the structure member variable name.
    Borrowing from the above example of structure nesting:
    insert image description here

sis ta member variable, a, b, cis sa member variable, access a, b, cthen t.s.a, t.s.b,t.s.c

  1. ->Operator
    Structure variable members can also be accessed ->through the operator, which has two operands, the left side is the structure pointer variable, and the right side is the structure member variable.
struct T t = {
    
     {
    
    100, 'w', 3.14}, "张三", 17 };
struct T* pt = &t;//结构体指针变量pt
//1.对结构体指针解引用找到指针所指向的结构体
printf("%d %c %lf %s %d\n", (*pt).s.a, (*pt).s.c, (*pt).s.d, (*pt).name, (*pt).num);
//2.通过结构体指针直接获得成员变量
printf("%d %c %lf %s %d\n", pt->s.a, pt->s.c, pt->s.d, pt->name, pt->num);

Fourth, the structure parameters

#include<stdio.h>
struct S
{
    
    
	int data[1000];
	int num;
};//结构体类型的声明

//结构体传参
void print1(struct S s)
{
    
    
	printf("%d\n", s.num);
}
//结构体地址传参
void print2(struct S* ps)
{
    
    
	printf("%d\n", ps->num);
}
int main()
{
    
    
	struct S s = {
    
     {
    
    1,2,3,4}, 1000 };
	print1(s);  //传结构体
	print2(&s); //传地址
	return 0;
}

Which of the above print1and print2functions is better?
The answer is: the preferred print2function.
reason:

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.

in conclusion:

When passing parameters to a structure, it is recommended to pass the address of the structure.

Guess you like

Origin blog.csdn.net/weixin_50614301/article/details/120357820