current position:Home>C language structure

C language structure

2022-08-06 09:32:33The ants look up to the sky

前言

Today we will look at structs,We know that an array is a collection of elements of the same type,But in everyday life we ​​often have to write many different types of elements together,For example the title of a book、作者、价格、release date, etc.,This is where we need to use structs.

结构体的声明

结构体的基础知识

结构体是一些值的集合,这些值称为成员变量.结构体的每个成员可以是不同类型的变量.

结构体声明

struct tag
{
    
	member - list;
}variable - list;

例如描述一个学生:

struct Stu
{
    
	char name[20];//名字
	int age;//年龄
	char sex[5];//性别
	char id[20];//学号
}Stu;//分号不能丢

结构体成员的类型

结构的成员可以是变量、数组、指针,甚至是其他结构体.

结构体变量的定义和初始化

有了结构体类型,那如何定义变量,其实很简单.

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

struct Point p3 = {
    5, 6};//初始化:定义变量的同时赋初值.这里5会赋值给x,6会赋值给y.

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

struct Node
{
    
    int data;
    struct Point p;//The above is nested herePoint结构体
    struct Node* next;//结构体指针,这个后续会讲,多用于C语言的数据结构
}n1 = {
    10, {
    4,5}, NULL}; //结构体嵌套初始化
struct Node n2 = {
    20, {
    5, 6}, NULL};//结构体嵌套初始化

结构体成员的访问

结构体变量访问成员
结构变量的成员是通过点操作符(.)访问的.点操作符接受两个操作数.

#include <stdio.h>
struct Point
{
    
	int x;
	int y;
}p1;//声明类型的同时定义变量p1,Don't forget the semicolon here
struct Point p2;//定义结构体变量p2
int main()
{
    
	p1.x = 10;//.操作符
	p1.y = 20;
	printf("%d %d ", p1.x, p1.y);
	return 0;
}

在这里插入图片描述
Here you can see that the structure variable is assigned first,And print the structure variables are used.操作符.

#include <stdio.h>
#include <string.h>
struct Stu
{
    
	char name[20];
	int age;
};
struct Stu s;
int main()
{
    
	strcpy(s.name, "zhangsan");//使用.访问name成员,Just pay attention to the use of assigning strings to structure variablesstrcpy函数
	s.age = 20;//使用.访问age成员
	printf("%s %d ", s.name, s.age);
	return 0;
}

在这里插入图片描述
以上为.操作符用法,and assigning strings to structure variables.
There are other times when we use struct pointers,At this time, if we want to use the variables in the structure, we need to use other operators,以下为代码示例:

#include <stdio.h>
#include <string.h>
struct Stu
{
    
	char name[20];
	int age;
};
void print(struct Stu* ps)
{
    
	printf("name = %s age = %d\n", (*ps).name, (*ps).age);
	//使用结构体指针访问指向对象的成员
	printf("name = %s age = %d\n", ps->name, ps->age);
}
int main()
{
    
	struct Stu s = {
     "zhangsan", 20 };
	print(&s);//结构体地址传参
	return 0;
}

在这里插入图片描述
如上代码所示,The two methods when using struct pointers are**(ps).变量名ps->变量名*,But in future programming we usually default to the latter.

结构体传参

这里我们直接看代码:

#include <stdio.h>
#include <string.h>
struct S
{
    
	int data[1000];
	int num;
};
struct S s = {
     {
    1,2,3,4}, 1000 };//结构体赋值
//Structure passed value parameters
void print1(struct S s)
{
    
	printf("%d\n", s.num);
}
//结构体地址传参
void print2(struct S* ps)
{
    
	printf("%d\n", ps->num);
}
int main()
{
    
	print1(s); //传结构体
	print2(&s); //传地址
	return 0;
}

在这里插入图片描述
上面的 print1 和 print2 函数哪个好些?
答案是:首选print2函数.
原因:
函数传参的时候,参数是需要压栈的.
如果传递一个结构体对象的时候,结构体过大,参数压栈的的系统开销比较大,所以会导致性能的下降.
As the above structure has five shapes元素, Assignment may be required when calling by value20字节的空间,And only when passing the address4个字节就可以(Because in a certain wayx86pointers in the programming environment4个字节),So if you call by reference, it will greatly save memory space.
结论:
结构体传参的时候,It is best to pass the address of the structure.
Looking forward to your last trip,如有错误,Welcome to private message or correct me in the comment section.

copyright notice
author[The ants look up to the sky],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/218/202208060926378618.html

Random recommended