[C language inspector training camp eighth day] those things about the structure

1. Basic concept of structure

Sometimes it is necessary to combine different types of data into a whole for easy reference. For example, a student has attributes such as student number, name, gender, age, address, etc. If a variable is defined separately for the student's student number, name, age, etc., it will be difficult to distinguish the variables when there are multiple students. To this end, the C language provides structures to manage different types of data combinations.

The general form of declaring a struct type is

struct structure name
{member table column};

For example,

struct student {
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
};

Declare the structure type first, and then define the variable name. For example,
struct student student1, student2;
see the following example:

#include <stdio.h>
struct student{
    
    
	int num;
	char name[20];
	char sex;
	int age;
	float score;
	char addr[30];
}; //结构体类型声明,注意最后一定要加分号
int main()
{
    
    
	struct student s={
    
    1001,"lele",'M',20,85.4,"Shenzhen"}; //定义及初始化
	struct student sarr[3];
	int i;
	printf("%d %s %c %d %f %s\n",s.num,s.name,s.sex,s.age,s.score,s.addr);
	for(i=0;i<3;i++)
	{
    
    
		scanf("%d%s %c%d%f%s",&sarr[i].num,sarr[i].name,&sarr[i].sex,&sarr[i].age, &sarr[i].score,sarr[i].addr);
	}
	for(i=0;i<3;i++)
	{
    
    
		printf("%d %s %c %d %f %s\n",sarr[i].num,sarr[i].name,sarr[i].sex, sarr[i].age,sarr[i].score,sarr[i].addr);
	}
	return 0;
}

The structure type declaration should be placed before the main function , so that the structure can be used in the main function. In the work, the structure declaration is often placed in the header file. Note that a semicolon must be added at the end of the structure type declaration, otherwise it will fail to compile. In addition, when defining a structure variable, use struct student to define, not only struct or student, otherwise it will fail to compile, and sarr is a structure array variable. The initialization of the structure can only be defined at the beginning. If struct student s={1001,"lele",'M',20,85.4,"Shenzhen"} has been executed, that is, struct student s has been defined, and s cannot be executed again ={1001,"lele",'M',20,85.4,"Shenzhen"}. If the structure variable has been defined, it can only be assigned to each member individually , such as s.num=1003.

2. Calculation method of structure size (alignment)

There are several alignment rules for the structure itself, which are difficult to remember, and it is completely unnecessary for the preliminary examination of the postgraduate entrance examination. You only need to remember one for the preliminary examination of the postgraduate entrance examination.结构体的大小必须是其最大成员的整数倍!

In the declaration without the macro #pragma pack, follow the following three principles:

  • 1. The initial address of the first member is 0.
  • 2. The first address of each member is an integer multiple of its own size
  • 3. The total size of the structure is an integer multiple of the largest type contained in its members.
#include <stdio.h>
struct student_type1{
    
    
	double score;//double 是一种浮点类型,8 个字节,浮点分为 float 和 double,记住有这两种即可
	short age;
};
struct student_type2{
    
    
	double score;
	int height;
	short age;
};
struct student_type3{
    
    
	int height;
	char sex;
	short age;
};
int main() {
    
    
	struct student_type1 s1;
	struct student_type2 s2;
	struct student_type3 s3;
	printf("s1 size=%d\n",sizeof(s1));
	printf("s2 size=%d\n",sizeof(s2));
	printf("s3 size=%d\n",sizeof(s3));
	return 0;
}

insert image description here

3. How to use the structure pointer

A pointer to a structure variable is the starting address of the memory segment occupied by the variable. You can set a pointer variable and
use it to point to a structure variable. At this time, the value of the pointer variable is the starting address of the structure variable. The pointer variable can also be used to
point to the elements in the structure array, so that each member in the structure can be quickly accessed through the structure pointer.

#include <stdio.h>
//结构体指针
struct student{
    
    
	int num;
	char name[20];
	char sex;
};
int main()
{
    
    
	struct student s={
    
    1001,"wangle",'M'};
	struct student sarr[3]={
    
    1001,"lilei",'M',1005,"zhangsan",'M',1007,"lili",'F'};
	struct student *p; //定义结构体指针
	int num;
	p=&s;
	printf("%d %s %c\n",p->num,p->name,p->sex);
	p=sarr;
	printf("%d %s %c\n",sarr[0].num,sarr[0].name,sarr[0].sex); //方式一获取成员
	printf("%d %s %c\n",(*p).num,(*p).name,(*p).sex); //方式一获取成员
	printf("%d %s %c\n",p->num,p->name,p->sex); //方式二获取成员
	printf("------------------------------\n");
	p=p+1;
	printf("%d %s %c\n",p->num,p->name,p->sex);
	return 0;
}

p is a structure pointer, you can take the address of the structure s and assign it to p, so with the member selection operator, you can access each member of the structure through p, and then print. We know that the first address of the data is stored in the array name, so we can assign sarr to p, so that the corresponding members can be accessed in two ways. Why use parentheses to access members using ( p).num? The reason is that the priority of "." member selection is higher than that of " " (that is, the value) operator, so parentheses must be added, get sarr[0] through *p, and then get the corresponding result

4. Structure renaming

The statement used to define the structure variable earlier is struct student s. It is a little troublesome to define the structure variable in this way, that is, you need to write struct student every time. So is there a simpler way to define it? The answer is yes, you can choose to use typedef to declare a new type name to replace the existing type name.

#include <stdio.h>
//结构体指针
typedef struct student{
    
    
	int num;
	char name[20];
	char sex;
}stu,*pstu;
typedef int INTEGER;
int main()
{
    
    
	stu s={
    
    1001,"wangle",'M'};
	pstu p;
	INTEGER i=10;
	p=&s;
	printf("i=%d,p->num=%d\n",i,p->num);
	return 0;
}

Using stu to define structure variables is equivalent to using struct student to define structure variables; using INTEGER to define variable i is equivalent to using int to define variable i; pstu is equivalent to struct student*, so p is a structure pointer variable .


insert image description here

Guess you like

Origin blog.csdn.net/apple_51931783/article/details/129893792