C Language_(10)_Constructed Data Types_Structure (1)

        In the previous, we have learned the basic data types of C language, such as integer type, floating point type, character type, default type

        In C language, we can construct data types. There are structures, unions, and enumerations.

Table of contents

 1 Definition of structure

 2 Definition of structure variables

 3 Access to structure elements

 4 Structure initialization


 1 Definition of structure

            When we first learned C language, the objects we manipulated were basically numbers and characters. Then we want to construct some types of words, such as computer, kitten, puppy, person, age, time. Then we can use the keyword struct to construct the type we want. If a kitten type is constructed, it will have kitten A, kitten B, kitten C, etc... We will use the structure.

Definition of structure type

       struct structure name {

                        Data type 1 member variable 1;

                        Data type 2 member variable 2;

                        data type 3 member variable 3;

                        Data type 4 member variable 4;

                        . . . . . . 

};

Define a new type, this type is called struct student, not student

//定义了一个学生的类型(姓名,性别,年龄,学号)
struct student{
	char name[32];
	char sex;
	int age;
	int score;
};

Of course, structures can also be defined nested

//定义一个日期的类型
struct date{
	int year;
	int month;
	int day;
};
//定义一个时间的类型
struct time{
	int hour;
	int min;
	int sec;
};
//定义一个既有时间又有日期的类型(结构体嵌套定义)
struct date_time{
	struct date DATE;
	struct time TIME;
};

 Another possibility is that the type defined by itself nests itself, which will nest infinitely, and will seriously crash.

struct node{
    struct node n;
};

Because we don't know that there is no fixed space for this unknown type, but if we take it as a pointer, the above situation will not happen, because the pointer will have a fixed size of 8 bytes.

struct node{
    struct node *n;
};

  2 Definition of structure variables

        storage type data type variable name;

Definitions can be defined as local variables and global variables, both are fine. pointers can also

struct student Tom;//定义一个学生Tom
struct student Jack;//定义杰克
struct student *p_Jack;//定义杰克

Jack and Tom are independent, but both of them are students.

And sometimes we can directly define it behind the structure, there can be pointers, and there can be variables.

//定义了一个学生的类型(姓名,性别,年龄,学号)
struct student{
	char name[32];
	char sex;
	int age;
	int score;
}Tom,Jack,p_Jack;

   3 Access to structure elements

Variables. Access members of structs

Pointer -> Access members of the structure    

struct student Tom;//定义一个学生Tom
struct student Jack;//定义杰克
strcpy(Jack.name,"Jack");//数组的数组名不是一个变量,所以要用字符串赋值函数strcpy
Jack.sex = 'M';
Jack.age = 18;

This is the name, gender and age of visiting Jack

Then the type of structure access member variable expression is determined by the member variable type.

 4 Structure initialization

All initialized

struct student Jack = {"Jack",'m',19,60};
struct date_time dt = {
    
    {2022,11,3},{18,18,18}};

local initialization

struct    student Tom = {

        .name = "Tom",

        .score = 100

} ;

The remaining unspecified elements are all 0 values

Local initialization of nested structures

struct      date_time = {

        .DATE = {

                .year = 2022,

},

        .TIME = {

                .hour = 18,

                .min = 18,

}

};

Based on the above synthesis, come to a code example for practical operation

#include <stdio.h>
#include <string.h>

//定义一个日期的类型
struct date{
	int year;
	int month;
	int day;
};
//定义一个时间的类型
struct time{
	int hour;
	int min;
	int sec;
};
//定义一个既有时间又有日期的类型(结构体嵌套定义)
struct date_time{
	struct date DATE;
	struct time TIME;
};
//定义了一个学生的类型(姓名,性别,年龄,学号)
struct student{
	char name[32];
	char sex;
	int age;
	int score;
};

int main(int argc, const char *argv[])
{
	struct student Tom;//定义一个学生Tom
	struct student Jack = {"Jack",'m',19,60};
	struct date_time dt = {
   
   {0,0,0},{0,0,0}};
	strcpy(Tom.name,"Tom");
	Tom.sex = 'M';
	Tom.age = 18;
	Tom.score = 100;

	printf("Tom的姓名%s\n",Tom.name);
	printf("Tom的性别%c\n",Tom.sex);
	printf("Tom的年龄%d\n",Tom.age);
	printf("Tom的成绩%d\n",Tom.score);
	printf("========================================\n");
	printf("Jack的姓名%s\n",Jack.name);
	printf("Jack的性别%c\n",Jack.sex);
	printf("Jack的年龄%d\n",Jack.age);
	printf("Jack的成绩%d\n",Jack.score);
	printf("========================================\n");
	
	dt.DATE.year = 2022;
	dt.DATE.month = 11;
	dt.DATE.day = 3;
	dt.TIME.hour = 18;
	dt.TIME.min = 18;
	dt.TIME.sec = 18;

	printf("现在是%d年%d月%d日%d时%d分%d秒\n",
			dt.DATE.year,	
			dt.DATE.month,	
			dt.DATE.day,	
			dt.TIME.hour,	
			dt.TIME.min,	
			dt.TIME.sec);

	return 0;
}

 

おすすめ

転載: blog.csdn.net/m0_58193842/article/details/127673195