[C Language] Structure Detailed Explanation

Table of contents

1. Definition of structure type

 2. Definition and initialization of structure variables

3. Access to structure members

1. Structure variable access members

2. The structure pointer accesses the member of the variable 

4. Structure parameter passing


In real life, a thing will have many attributes connected. The C language introduces a structured data type - structure

Organize multiple data belonging to a thing to reflect its internal relationship.

1. Definition of structure type

A structure type is a constructed type, which is composed of several members, and each member can be a basic data type or a constructed type.

The name of a structure type is composed of a keyword struct and the name of the structure

The general form of a structure definition:

struct structure type name

{

        type specifier 1 member name 1;

        type specifier 2 member-name 2;

        ...

};

struct Stu 
{
	int age;
	int ID;
	char name[10];
};

typedef type renaming

Notice:

  1. struct is a structure keyword and cannot be omitted. The structure type name can be omitted and is called an unnamed structure. Structure members can be any basic data type, as well as array and pointer types
  2. The statement defining the structure type should end with ;,  pay attention to the ; after {}
  3. At compile time, the system does not allocate memory space for the defined structure type
  4. The structure type is the same as the variable, and the scope is also divided into global and local

 2. Definition and initialization of structure variables

struct Point
{
	int x;
	int y;
}p1;
struct Point p2;

 (1) Define the structure type first, and then define the structure variable

struct Stu 
{
	int age;
	char name[10];
};
int main()
{
	//struct 结构体类型的名称 结构体变量名的列表;
	struct Stu stu1,stu2;
	return 0;
}

(2) Define the structure variable while defining the structure type

struct structure type name

{

        member list;

} List of structure variable names;

struct Point
{
	int x;
	int y;
}p1,pn;

(3) Directly define structure variables

struct 
{
	int age;
	int num;
	char sex;
}s1,s2;

Initialization can specify member initialization 

3. Access to structure members

1. Structure variable access members

Members of structure variables are accessed with the dot operator ( . ). The dot operator takes two operands

#include<stdio.h>
#include<string.h>
struct Stu
{
	int age;
	char name[10];
};
int main()
{
	struct Stu s;
	s.age = 18;
	strcpy(s.name,"zhangsan");
	printf("%s %d",s.name,s.age);
	return 0;
}

【Result】zhangsan 18

We can also see from debugging

 

2. The structure pointer accesses the member of the variable 

#include<stdio.h>
struct Stu
{	
	char name[10];
	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",18};
	Print(&s);
	return 0;
}

 If you just print members, you can not pass the address

(It is recommended to pass the address, because the operation efficiency is high)

4. Structure parameter passing

#include<stdio.h>
struct S 
{
	int data[100];
	int num;
};
struct S s = { {1,2,3},100 };
//结构体传参
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;
}

【Result】100 100

According to the above code print2 function is better

Because when a function passes parameters, 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 it will lead to a decrease in performance

When the structure is passed as a parameter, the address of the structure must be passed 

Guess you like

Origin blog.csdn.net/qq_72505850/article/details/132207842