[C language foundation 12 - structure]


foreword

This article begins to learn the knowledge points of the structure, the main contents include:

  • struct type declaration
  • structure initialization
  • struct member access
  • structure parameter

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 struct can be a variable of a different type

1.2 Declaration of structure

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

The following methods are the same, define the structure type of the student, including: name, age, gender, student number

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

//举例2
struct Stu
{
    
    
	char name[20];//名字
	int age;//年龄
	char sex[5];//性别
	char id[20];//学号
}s1,s2,s3;//定义了3个结构体类型的变量

//举例3
typedef struct stu stu;

//举例4
typedef struct stu
{
    
    
	char name[20];
	int age;
	char sex[8];
	float score;
}stu;

int main()
{
    
    
	struct stu stu4;//局部变量
	struct stu stu5;
	stu stu6;//全局变量
	return 0;
}

1.3 Types of Structure Members

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

1.4 Definition and initialization of structure 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

2.1 Dot operator access

  • Members of structure variables are accessed through the dot operator (.). The dot operator accepts two operands: variablename.member
struct S s;
strcpy(s.name, "zhangsan");//使用.访问name成员
s.age = 20;//使用.访问age成员

2.2 -> operator access

  • A pointer to a structure, a structure pointer accesses a member of a variable, variable name -> member
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);
}

3. Structure parameters

3.1 Parameters are variables of structure type

struct point
{
    
    
	int x;
	int y;
}p1 = {
    
    10,20};

struct s//结构体
{
    
    
	char c;
	struct point sp;
	double d;
	char arr[20];//字符串
};

void print1(struct s ss)//传参结构体
{
    
    
	printf("%c\n", ss.c);
	printf("%d\n", ss.sp.x);
	printf("%d\n", ss.sp.y);
	printf("%lf\n", ss.d);
	printf("%s\n", ss.arr);
}
int main()
{
    
    
	struct point p1 = {
    
     100, 200 };//定义1个变量,并初始化
	struct s ss = {
    
     'w',{
    
    100,20},5.5,"hello" };//初始化
	ss.c = 'b';
	ss.sp.x = 1000;
	ss.sp.y = 2000;
	ss.d = 3.14;
	//ss.arr = "wolrd";错误的方法
	strcpy(ss.arr, "world");//字符串赋值函数
	print1(ss);//打印结构体变量
}

3.2 The parameter is the address of a variable of structure type

struct point
{
    
    
	int x;
	int y;
}p1 = {
    
    10,20};

struct s
{
    
    
	char c;
	struct point sp;
	double d;
	char arr[20];//字符串
};
void print2(struct s* ss)//传参地址
{
    
    
	printf("%c\n", ss->c);
	printf("%d\n", ss->sp.x);
	printf("%d\n", ss->sp.y);
	printf("%lf\n", ss->d);
	printf("%s\n", ss->arr);
}
int main()
{
    
    
	struct s ss = {
    
     'w',{
    
    100,20},5.5,"hello" };//初始化
	ss.c = 'b';
	ss.sp.x = 1000;
	ss.sp.y = 2000;
	ss.d = 3.14;
	//ss.arr = "wolrd";错误的方法
	strcpy(ss.arr, "world");
	print2(&ss);//打印结构体变量
}

3.3 Structure parameter comparison

The above two functions achieve the same effect, as shown in the following figure:
insert image description here

  • The function print1 accepts a structure type variable, which is a formal parameter. In addition, it opens up space and copies the actual parameter , which takes up a lot of memory space. The
    insert image description here
    function print2 accepts the address of the structure type and defines the pointer of the structure type. Address , only open up space to store the address, occupying a small memory space
    insert image description here
    **Note:** When passing parameters to a function, the parameters need to be pushed on 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. Therefore, when the structure is passed parameters, the address of the structure should be passed, that is, the function print2 is more effective than the function print1.

Summarize

The structure has less content, so keep in mind the definition form and the usage of passing addresses.

The next article will learn some VS debugging skills. If there are bugs in programming, you can find problems through debugging and improve the program. This step is very important. While learning a language, the use of appropriate learning tools can achieve twice the result with half the effort.

Guess you like

Origin blog.csdn.net/taibudong1991/article/details/123968636