"C Language Elementary" Chapter 9 - Structure

insert image description here

foreword

Today Xiaoyang is here to share with you about the structure of the C language. In the C language, the structure type belongs to a structural type (other structural types include: array type, union type). Today we mainly understand briefly Look at the structure.


1. What is a structure?

In practical problems, a set of data often has many different data types. For example, to register student information, you may need to use the name of char type, the student number of type int or char, the age of type int, the gender of type char, and the grade of type float. For another example, to record a book, the title of the char type, the author name of the char type, and the price of the float type are required. In these cases, it is difficult to use simple primitive data types or even arrays. The structure can effectively solve this problem.
A structure is essentially a data type, but it can include several "members", each member's type can be the same or different, or it can be a basic data type or another constructed type.
The advantages of the structure: the structure can not only record different types of data, but also make the data structure "high cohesion, low coupling", which is more conducive to the reading comprehension and transplantation of the program, and the storage method of the structure can improve the performance of the CPU on the memory. access speed.
In simple terms, a structure is a collection of values ​​called member variables, and each member of the structure can be a variable of a different type.


2. Structure declaration

General form:

struct 结构体类型名
{
    
    
	成员列表
};

Therefore, the struct keyword means that the next is a structure

For example, to declare a Student struct:

struct Student{
    
             //声明结构体
    char name[20];      //姓名
    int num;            //学号
    float score;        //成绩
};

statement above 描述了一个包含三个不同类型的成员的结构,但它还没创建一个实际的数据对象,每个成员变量都用自己的声明来描述,以分号结束。花括号之后的分号表示结构声明结束. The structure declaration can be placed outside the function (this is a global structure, similar 全局变量, all functions declared after it can be used), or it can be placed inside the function (this is a local structure, similar 局部变量, it can only be placed in this If used within a function, if it has the same name as the global structure, the global structure will be temporarily blocked).


3. Definition and initialization of structure variables

After we declare the type of the structure, the next step is to define and initialize the structure i.

struct student
{
    
    
	char name[20];
	int age;
	char sex[5];
	char id[20];
}s1,s2;           //声明类型的同时定义结构体变量
 
//定义结构体变量
struct student s3;
struct student s4;
int main()
{
    
    
	return 0;
}

The above s1, s2, s3, and s4 are ordinary variables, which are global variables when placed globally, and local variables just inside the function.
Next, look at the structure initialization:

struct student
{
    
    
	char name[20];
	int age;
	char sex[5];
	char id[20];
}s1 = {
    
     "zhuxinrui",21,"女","21006666" };
int main()
{
    
    
	//初始化:定义变量的同时赋值
	struct student s2 = {
    
     "yangrui",21,"女","210047218" };
	return 0;
}

1. typedef keyword

当我们每次定义结构体变量时,结构体的类型名过长,想要简化可以使用typedef来对结构体类型重定义

1:>

#include<stdio.h>
typedef struct student stu;
struct student
{
    
    
	char name[20];
	int age;
	char sex[5];
	char id[20];
};
int main()
{
    
    
	stu s3 = {
    
     "yangrui",21,"女","210047218" };
	return 0;
}

2:>

#include<stdio.h>
typedef struct student
{
    
    
	char name[20];
	int age;
	char sex[5];
	char id[20];
}stu;
int main()
{
    
    
	stu s4 = {
    
     "yangrui",21,"女","210047218" };
	return 0;
}

2. Anonymous structure

Anonymous structure type means that when declaring a structure, it can be incompletely declared

struct 
{
    
    
	int num;
};

So the question is, how to define variables for anonymous structure types?

错误写法

struct
{
    
    
	int num;
};
 
struct n1;
struct n2 = {
    
     666 };

正确做法

struct
{
    
    
	int num;
}n1,n2={
    
    666};

Summarize:
必须在声明匿名结构体类型时就定义变量。

4. Structure parameter passing

When we pass structure variables as function parameters, how should we receive and use them?

usage example

struct print function

#include<stdio.h>

typedef struct teacher T;
typedef struct student S;

struct teacher
{
    
    
	char name[20];
	char subject[20];

};

struct student
{
    
    
	char name[20];
	int age;
	char sex[5];
	char id[20];
	T t1;
};

S s1 = {
    
     "阿蕊",20,"女","21006666",{
    
    "老王","语文"} };


void print1(S s)
{
    
    
	printf("%-8s %-2d %s %s %s %s\n", s.name, s.age, s.sex, s.id, s.t1.name, s.t1.subject);
}

void print2(S* s)
{
    
    
	printf("%-8s %-2d %s %s %s %s\n", (*s).name, (*s).age, (*s).sex, (*s).id, (*s).t1.name, (*s).t1.subject);
}

int main()
{
    
    
	S s2 = {
    
     "小羊",21,"女","210047218",{
    
    "老丁","英语"} };
	print1(s1);
	print2(&s2);
	return 0;
}

Here comes the question, think about the two functions print1 and print2, which one is better?

Friends, if you think about it or don’t understand it, you can ask Xiaoyang, or you can leave a message in the comment area. Finally, I hope this article will be helpful to everyone on the road of learning C language. Follow along, and we will soon Will follow the advanced content of the new C language~

insert image description here

Guess you like

Origin blog.csdn.net/hsjsiwkwm/article/details/132288855