C language programming - structure (Part II)

Posted by maniac1aw on Sun, 19 Dec 2021 14:24:14 +0100

C language programming - sorting notes of structure. If there are errors, please correct them.
C language programming - structure (Part I)

Structure array

  • A set of associated data (such as a student's student number, name, grade, etc.) can be stored in a structural variable. If the data of 10 students need to participate in the operation, it is obvious that an array should be used, which is a structure array. Each array element of a structure array is data of a structure type, and they all include each member item.
  1. The general form of defining structure array is:
    (1) struct structure name
    {member table column} array name [array length]
    (2) First declare a structure type, and then use this type to define the structure array
    For example: struct person leader [3]// Leader is the structure array name

  2. The form of initializing the array of the structure is to add = {initial value table column} after the definition array;
    Example: struct person leader [3] = {"Li", 0, "Zhang", 0, "fun", 0};

Example 7: there are three candidates, and each voter can only vote for one person. Write a program for counting votes, input the names of the candidates successively, and finally output the results of each person's vote.

#include <stdio.h >
#include <string.h>
struct person //Declare structure type struct person
{
	char name[20]; //Name of candidate
	int count; //Number of votes obtained by candidates
}leader[3] = { "Li", 0, "Zhang", 0, "Fun", 0 }; //Define structure array and initialize

int main()
{
	int i, j;
	char leader_name[20]; //Define character array
	for (i = 1; i <= 10; i++)
	{
		scanf("%s", leader_name);//Enter the name of the selected candidate
		for (j = 0; j < 3; j++)
			if (strcmp(leader_name, leader[j].name) == 0)
				leader[j].count++; // If the name entered is the same as the name member in an element, add 1 to the count member of the element
	}
	printf("\nResult:\n");
	for (i = 0; i < 3; i++)
		printf("%5s:%d\n", leader[i].name, leader[i].count); //Outputs information from all elements of the array
	return 0;
}

Program analysis:

  • ! Due to member operator '.' It takes precedence over the autoincrement operator "+ +", so it is equivalent to (leader[j].count) + +, increasing the value of leader[j] member count by 1.
  • C language allows global assignment of variables with the same structure type. Note: strcpy() must be used when assigning values to members of character array structures.
    Example: strcpy(stu1.studentName, "Wang Gang");, Instead of stu2 studentName = stu1. studentName. Because the structure member studentname is a character array, studentname is the name of the array, represents the first address of the character array, is a constant, and cannot be used as the lvalue of the assignment expression.

Example 8: there are N students' information (including student number, name and grade). It is required to output the information of each student in the order of grade. (sequential method)

#include<stdio.h>
#define N 5
struct student //Declare structure type struct student
{
	int num;
	char name[20];
	float score;
};
int main()
{
	struct student stu[N]; //Define structure array
	struct student temp; //Defines the structure variable temp, which is used as a temporary variable during exchange
	int i, j, k;
	for (i = 0; i < N; i++)
	scanf("%d %s %f", &stu[i].num, stu[i].name, &stu[i].score);
	printf("The order is: \n");
	for (i = 0; i < N - 1; i++)
		for (j = i + 1; j < N; j++)
		{
			k = i;
			if (stu[j].score > stu[k].score) //Compare the results
			{
				k = j;
				temp = stu[k]; stu[k] = stu[i]; stu[i] = temp; //stu[k] and stu[i] elements are interchangeable as a whole
			}
		}
	for (i = 0; i < N; i++)
		printf("%6d %8s %7.2f\n", stu[i].num, stu[i].name, stu[i].score); // Output results
	printf("\n");
	return 0;
}

Program analysis:

  • The program can be applied to any student. Now N is defined as 5. If there are 30 students, just change the second line to "#define N 30", and the other lines do not need to be modified.
  • ! Note: the temporary variable temp should also be defined as struct student type. Only structural variables of the same type can be assigned to each other (all members are interchangeable as a whole, and it is not necessary to specify one member to be interchangeable artificially). From this point, we can also see the benefits of using structural type.
Structure pointer
  • The so-called structure pointer is the pointer to the structure data. The starting address of a structure variable is the pointer to the structure variable.
  • If the starting address of a structure variable is stored in a pointer variable, the pointer variable points to the structure variable.
  • Pointer variables can be used to point to structure variables or to point to elements in the structure array. However, the base type of the pointer variable must be the same as that of the structure variable.

Example 9: output the information of the members in the structure variable through the pointer variable pointing to the structure variable.

#include< stdio.h>
#include <string.h>
int main()
{
	struct student
	{
		long num;
		char name[20];
		float score;
	};
	struct student stu1; //Defines the variable stu1 of type struct student
	struct student * p; //Defines a pointer variable p that points to data of type struct studen t
	p = &stu1; //p points to the structure variable stu1
	stu1.num = 10101; //Assign a value to the num member of the structure variable
	strcpy(stu1.name, "Li Lin"); // The value of the name member of the structure variable
	stu1.score = 89.5; //Assignment of score member of structure variable
	printf("No. :%ld\nname: %s\nsex:%c\nscore:%5.1f\n", stu1. num, stu1.name,stu1.score); //Output the value of each member
	printf("\nNo. :%ld\nname: %s\nsex:%c\nscore:%5.1f\n", (* p).num, (* p).name, (* p).score); //Output the value of each member
	return 0;
}

Program analysis:

  • Declare the struct student type in the main function, then define a variable stu1 of struct student type and a pointer variable p, which points to the data of a struct student type. Assign the starting address of the structure variable stu1 to the pointer variable p, that is, make P point to stu1, and then assign values to each member of stu1. The first printf function accesses its members through the structure variable name stu1 and outputs the values of each member of stu1. Use stu1 Num represents the member num in stu1, and so on. The second printf accesses its members through the pointer variable pointing to the structure variable, outputs the values of stu1 members, and uses (* p) In the form of num. (* p) indicates the structural variable pointed to by P, (* p) Num is the member num in the structure variable pointed to by P.
  • Note that the parentheses around * P cannot be omitted because the member operator "." It takes precedence over the "" operator. Without parentheses, p.num is equivalent to (p.num).
  • Note: in C language, p} > num is allowed to replace (* p) Num, which represents the num member in the structure variable pointed to by p. Similarly, p} > name is equivalent to (* p) name. "ー >" is called a pointing operator.

If p points to a structure variable, the following three forms are equivalent:

  • Structure variables member name
  • (* p). member name
  • P - > member name

Pointer variables that point to structural variables can also be used to point to structural array elements.

Example 10: the information of three students is placed in the structure array. It is required to output the information of all students.

#include<stdio.h>
struct student //Declare structure type struct student
{
	int num;
	char name[15];
	int age;
};
struct student stu[3] = { {10101, "Li Lin", 18}, {10102, "Zhang Fun", 19},{10104, "Wang Min", 20} };
//Define structure array and initialize
int main()
{
	struct student* p; //Defines a pointer to a struct student struct variable
	printf("No.   Name         age\n");
	for (p = stu; p < stu + 3; p++)
		printf("%5d %-10s %4d\n", p->num, p->name, p->age); //Output results
	return 0;
}

Program analysis:

  • In the for statement, first make the initial value of p stu, that is, the starting address of the first element of the array stu.
  • In the first loop, each member value of stu[0] is output, and then p + + is executed to make p self add 1. p+1 means that the added value of P is the number of bytes occupied by an element of the structure array stu (the number of bytes occupied by an element is 4 + 15 + 1 + 4 = 24 bytes).
  • After executing p + +, the value of p is equal to stu+1, p points to stu[1], and the member values of stu[1] are output in the second loop.
  • After executing p + +, the value of p is equal to stu+2, and then the member values of stu[2] are output.
  • After p + + is executed again, the value of p becomes stu+3, which is no longer less than stu+3, and the loop is no longer executed.

be careful:

  • If the initial value of p is stu, that is, p points to the first element of stu. After p Plus 1, p points to the next element.
    Example:
(++P)->num //First, add 1 to p, and then get the num member value in the element pointed to by p (i.e. 10102)
(p++)->num //First obtain the value of P - > num (i.e. 10101), and then make p add 1 to point to stu[1]

  • The program has defined that p is a pointer variable pointing to the data of struct student type, which is used to point to the data of struct student type (the value of p is an element of stu array (such as the starting address of stu[0] and stu[1]), and should not be used to point to a member of stu array elements.
    Example: (error) ❌)
p=stu[1].name;
  • A warning message will be given at compile time, indicating that the type of address does not match.
  • ! Don't think that p is the storage address anyway. You can assign any address to it. If you want to assign a member's address to p, you can use forced type conversion to convert the member's address to the type of p.
    Example: P = (stu student *) stu[0] Name at this time, the value of P is the starting address of the name member of the stu[0] element. You can use "printf" (% s ", p;) "Output the value of member name in stu. But p keeps the original type. If" printf("%s",p+1); "The value of name in stu[1] will be output. When p + + is executed, the value of P increases the length of struct student.
Structure variables and pointers to structure variables are used as function parameters

There are three methods to transfer the value of a structure variable to another function:

  • Use the members of structure variables as parameters. For example, use stu [1] Num or stu [2] Name takes the function argument and passes the argument value to the formal parameter. The usage is the same as using ordinary variables as arguments. It belongs to the "value passing" method.! Note: the type of the argument is consistent with that of the formal parameter
  • Use structure variables as arguments. When using a structure variable as an argument, the method of "value transfer" is also adopted to transfer all the contents of the memory unit occupied by the structure variable to the formal parameter in order. The formal parameter must also be a structure variable of the same type, and the formal parameter also occupies the memory unit during function call. This transmission method has a large overhead in space and time. If the scale of the structure is large, the overhead is considerable. In addition, due to the value passing method, if the value of a formal parameter (also a structural variable) is changed during the execution of the called function, the value cannot be returned to the calling function, which often causes inconvenience in use. Therefore, this method is generally less used.
  • Use the pointer to the structure variable (or array) as the argument to pass the address of the structure variable (or array) to the formal parameter.

Example: there are N structural variables stu, including student number, name and grades of 3 courses. It is required to output the information of the student with the highest average score (including student number, name, score of 3 courses and average score).

#include<stdio.h>
#define N 3 / / the number of students is 3
struct student //Declare structure type struct student
{
	int num; //Student number
	char name[20]; //full name
	float score[3];  //Results of three courses
	float aver; //Average score
};

	int main()
	{
		void input(struct student stu[]); //Function declaration
		struct student max(struct student stu[]); //Function declaration
		void print(struct student stud); //Function declaration
		struct student stu[N], * p = stu; //Define structure arrays and pointers
		input(p); //Call the input function
		print(max(p)); //Call the print function with the return value of the max function as the argument
		return 0;
	}

	void input(struct student stu[]) //Define input function
	{
		int i;
		printf("Please enter the information of each student:Student number, name and grades of three courses:\n");
		for (i = 0; i < N; i++)
		{
			scanf("%d %s %f %f %f", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);//input data
			stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0; //Average score
		}
	}

	struct student max(struct student stu[]) // Define max function
	{
	int i, m = 0; //Use m to store the serial number of the student with the highest score in the array
	for (i = 0; i < N; i++)
		if (stu[i].aver > stu[m].aver) //Find the serial number of the student with the highest average score in the array
			m = i; 
	return stu[m]; //Returns the structure element containing the information
	}

void print(struct student stud) //Define print function
{
	printf("\n The students with the highest scores are:\n");
	printf("Student number:%d\n full name:%s\n Results of three courses:%5.1f,%5.1f,%5.1f\n Average score:%6.2f",
	 stud.num, stud.name, stud.score[0], stud.score[1], stud.score[2], stud.aver);
}

//Input:
10101 Li 78 89 98
10102 Wang 98.5 87 69
10103 Fun 88 76.5 89

//The output result is:
The students with the highest scores are:
Student number:10101
 full name:Li
 Results of three courses: 78.0, 89.0, 98.0
 Average score: 88.33

Program analysis:

  • In the main function, the array stu of struct student type and the pointer variable p pointing to the data of struct student type are defined, so that P points to the first element stu[0] of stu array.
  • When calling the input function, use the pointer variable p as the function argument. The formal parameter of the input function is an array stu of type struct student (! Note: the formal parameter array stu and the array stu in the main function are local data. Although they have the same name, they represent different objects before the virtual real combination of the calling function, and they have no relationship with each other). When the input function is called, the address of the first element of the stu array in the main function is passed to the formal parameter array stu, so that the shape parameter group stu has the same address as the stu array in the main function. Therefore, inputting data to the shape parameter group stu in the input function is equal to inputting data to the stu array in the main function. The input function has no return value. Its function is to assign a certain value to each element of the stu array.
  • After inputting data with scanf function, the average score of the student is calculated immediately, Stu [i] Aver stands for the average score of students with serial number I.
  • The print function is called in the main function, and the argument is Max. The calling process is to call the max function (with p as the argument) to get the value of Max § and then use it to call the print function.
  • Now analyze the process of calling the max function: as before, the pointer variable p passes the address of the first element of the stu array in the main function to the formal parameter array stu, so that the shape parameter group stu has the same address as the stu array in the main function. In the max function, the operation on the shape parameter group is the operation on the stu array in the main function. In the max function, compare the average score of each person with the current "highest average score", store the sequence number of the student with the highest average score in the array stu in the variable m, and return the value of stu[m] to the main function through the return statement.
  • ! Note: stu[m] is an element of a struct array. The max function is of type struct student.
  • The print function is called with the element whose value of max (P) is the structure array as an argument. The formal parameter of the print function is a variable of type struct student (not an array of type struct student). Virtual real combination is carried out during the call, and the value of stu[m] (which is a structural element) is passed to the formal parameter study. At this time, what is passed is not the address, but the information in the structural variable. Output the value of each member in the structure variable in the print function.
  • The calls of the above three functions are different:
  1. When calling the input function, the argument is the pointer variable p, the formal parameter is the array name of the structure, the address of the structure element is passed, and the function has no return value.
  2. When calling the max function, the argument is the pointer variable p, the formal parameter is the structure array name, the address of the structure element is passed, and the return value of the function is the structure type data.
  3. When calling the print function, the argument is the structure variable (structure array element), the formal parameter is the structure variable, the value of each member in the structure variable is passed, and the function has no return value.

Supplement:

typedef struct tagNode
{
  char *pItem;
  pNode pNext;
} *pNode;
  • Errors will be reported in the above code compilation stage. Reasons:
    In the above code, a pNext declaration is encountered during the establishment of the new structure, and its type is pNode. In particular, pNode represents the new alias of the structure.
  • Therefore, the problem arises. When the structure type itself has not been established, the compiler does not know pNode at all. Because the new alias of the structure type does not exist, an error will be reported naturally.
  • Therefore, we need to make some appropriate adjustments, such as modifying the pNext declaration in the structure to the following way:
typedef struct tagNode
{
  char *pItem;
  struct tagNode *pNext;
} *pNode;

Or define struct and typedef separately

typedef struct tagNode *pNode;
struct tagNode
{
  char *pItem;
  pNode pNext;
};
  • In the above code, we also use typedef to give a new alias to an undeclared type tagNode. However, although the C language compiler fully supports this approach, it is not recommended to do so. It is recommended to change it to:
struct tagNode
{
   char *pItem;
   struct tagNode *pNext;
};
typedef struct tagNode *pNode;

Topics: C Algorithm data structure Visual Studio