C language, structure

Posted by Zwiter on Wed, 29 Dec 2021 00:34:41 +0100

Look at a cat problem

Mrs. Zhang has two cats: one is Xiaobai, 3 years old, white. There is also a flower named Xiaohua, 100 years old this year. Please write a program to display the cat's name, age and color when the user enters the cat's name. If the kitten name entered by the user is wrong, it will show that Mrs. Zhang does not have this kitten.

Using traditional technology to solve

1) Separate definition variables are resolved
2) We learned about arrays, which are collections of data of the same type. In programming, we often need a group of data with different types. For example, the cat's name uses a string and the age is int. because of different data types, we can't store it in an array

Analysis of shortcomings solved by existing technologies

It is not conducive to data management and maintenance, because the three attributes of the cat (name, age and color) are a whole, and the traditional solution is to decompose them. = > structural morphology

Schematic diagram of the relationship between structure and structure variables

Quick start - object oriented approach (struct) to solve the cat problem

#include <stdio.h> 

void main() { 
	/*Mrs. Zhang has two cats: one is Xiaobai, 3 years old, white.
	There is also a flower named Xiaohua, 100 years old this year. 
	Please write a program. When the user enters the kitten's name,
	It displays the cat's name, age and color. 
	If the kitten name entered by the user is wrong, it will show that Mrs. Zhang does not have this kitten. 
	
	Analysis: 1 A cat consists of three members (variables) 
		   2. Solve with structure 
		   
	*/
	//Create structure [is data type] 
	
	struct Cat { // The structure name is cat. Cat is a data type constructed by ourselves 
		char * name; //Name, using a pointer to point to a string 
		int age; //Age 
		char *color; //colour 
	}; 
	
	//Using Cat structures, create variables 
	
	struct Cat cat1; // cat1 is a variable of struct Cat 
	struct Cat cat2; // cat2 is a variable of struct Cat 
	
	//Assign values to each member of cat1 
	cat1.name = "Xiaobai"; 
	cat1.age = 3; 
	cat1.color = "white"; 
	
	//Assign values to each member of cat2 
	
	cat2.name = "floret"; 
	cat2.age = 100; 
	cat2.color = "Decor"; 
	
	//Output information about two cats 
	printf("First cat name=%s age=%d color=%s", cat1.name, cat1.age, cat1.color); 
	printf("\n The second cat name=%s age=%d color=%s", cat2.name, cat2.age, cat2.color); 
	
}

Differences and relations between structure and structure variables

Through the above cases and explanations, we can see that:

1) A structure is a user - defined data type that represents a data type
2) Structure variables represent a specific variable, such as

int num1 ; // Int is a data type, and num1 is a concrete int variable 
struct Cat cat1; // Cat is a structure data type and cat1 is a cat variable

3) Cat is like a "template". The defined structure variables contain the same members. You can also compare the structure to "drawing" and the structure variable to "part". The characteristics of parts produced according to the same drawing are the same

How to declare a structure

struct Structure name { // Structure names are capitalized, such as cat and person 
				Member list; 
};

For example:

struct Student{ 
	char *name; //full name 
	int num; //Student number 
	int age; //Age 
	char group; //Study Group 
	float score; //achievement
	//Members can also be structures
};

member

Basic introduction
1) In terms of Name: some books are called members, and some books say that the structure contains variables
2) A member is an integral part of a structure. It is generally a basic data type, or an array, pointer, structure, etc. For example, the int age of the Cat structure we defined earlier is a member.

Notes and details

1) The syntax of member declaration is the same as that of variable. Example: data type member name;
2) The types of fields can be: basic type, array or pointer, structure, etc
3) After creating a structure variable, you need to assign a value to the member. If you use it without assignment, it may lead to abnormal termination of the program. [case presentation]:

#include <stdio.h> 

void main() { 
	struct Cat{ // It is recommended to capitalize the structure name 
		char *name; //Name, pointer type is required here 
		int age; //Age 
		char *color; // colour 
};
	struct Cat cat1; //A structure variable cat1 is defined 
	printf("\n name=%s Age=%d colour=%s ", cat1.name, cat1.age, cat1.color); 
}

Running the above code will cause the program to terminate abnormally because the members of the structure variables are not initialized

4) The members of different structural variables are independent and do not affect each other. Changing the members of one structural variable does not affect the other.

Create structure and structure variables

1) Method 1 - first define the structure, and then create the structure variable

struct Stu{ 
	char *name; //full name
	int num; //Student number 
	int age; //Age 
	char group; //Study Group 
	float score; //achievement 
}; 
	struct Stu stu1, stu2; //Two variables stu1 and stu2 are defined, both of which are Stu types and are composed of five members 
	//Note that the keyword struct cannot be less

2) Method 2 - structure variables are defined at the same time as the structure is defined

struct Stu{ 
	char *name; //full name 
	int num; //Student number 
	int age; //Age 
	char group; //Study Group 
	float score; //achievement 
} stu1, stu2; //While defining the structure Stu, two structure variables stu1 and stu2 are created

3) Method 3 - if only stu1 and stu2 variables are required, the structure data type is no longer required. Other variables can be defined without giving the structure name

struct { //No Stu 
	char *name; //full name
	int num; //Student number 
	int age; //Age 
	char group; //Study Group 
	float score; //achievement 
} stu1, stu2; 
	
	stu1.name = "tom"; 
	stu1.num = 100;.... //1. The data type of this structure has no name and is anonymous 
						//2. stu1 and stu2 are the two variables of the structure

Acquisition and assignment of members

Structure is similar to array. It is also a set of data. It doesn't make much sense to use it as a whole. The array uses the subscript [] to get a single element, and the structure uses the point number Gets a single member. The general format for obtaining structure members is structure variable name Member name;

Case 1

struct{ 
	char *name; //full name 
	int num; //Student number 
	int age; //Age 
	char group; //Group 
	float score; //achievement 
} stu1; 
	
	stu1.name = "jack"; printf("%s", stu1.name);

Case 2

#include <stdio.h> 

void main() { 
	struct Student{ 
		char *name; //full name 
		int num; //Student number 
		int age; //Age 
		char group; //Group 
		float score; //achievement 
} stu1 = {"Jia Baoyu", 11, 18, 'B', 90.50}, stu2 = { "Lin Daiyu", 12, 16, 'A', 100 }; 

	struct Student stu3 = {"Lin Daiyu 2", 12, 16, 'A', 100 }; //When defining structure variables, the overall assignment needs to be corresponding 

	struct Student stu4; 
	//stu4 = {"Lin Daiyu 2", 12, 16, 'A', 100}// This assignment is not allowed 
	//stu4.name = "smith"; 

	printf("\n stu1.name=%s", stu1.name); 
	printf("\n stu3.name=%s", stu3.name);

}

Topics: C Back-end