C language structure type definition + structure variable definition and use and its initialization + structure variable as function parameter

Keywords: C

Previous article: Function returning pointer value + pointer to function + parameter of main() function

structural morphology

Cited examples

Output the student information with the highest average score

#include <stdio.h>
struct student
{
	int num;
	char name[10];
	int computer,english,math;
	double average;//These are all structure members
};//Note that this semicolon should not be less, otherwise an error will be reported
int main()
{
	int i,n;
	struct student s1,max;//Define the structure variable s1 and the structure variable max
	printf("Input n:");
	scanf("%d",&n);//n is the number of students
	printf("Input the student's number, name and course scores:\n");
	for(i=1;i<=n;i++)//Using for loop to realize the input of n student grades
	{
		printf("No.%d:",i);//Prompt student's serial number
		scanf("%d%s%d%d%d",&s1.num,s1.name,&s1.math,&s1.english,&s1.computer);
		//Input values to the variables in the structure respectively
		s1.average=(s1.math+s1.english+s1.computer)/3.0;//Calculate the student's average score
		if(i==1)max=s1;
		if(max.average<s1.average)//Find the student with the highest average score
			max=s1;//Assign the values of the member variables in s1 to max respectively
	}
	printf("num:%d, name:%s, average:%.2lf\n",max.num,max.name,max.average);
	//Output the results of the students with the highest average scores
	return 0;	
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • Definition of structure type:
    Structure type is actually a template, which is defined as:
struct Structure name
{
	Type identifier structure member name 1;
	Type identifier structure member name 2;
	...
	Type identifier structure member name 3;
};//Don't forget the last semicolon

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Be careful not to forget the last semicolon

Definition of structure variables

When the structure type is well defined, note that the structure variable can be defined only when the structure type is well defined.
For example:

struct student
{
	int num;
	char name[10];
	int computer,english,math;
	double average;
};

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

This is the definition of structure type, and then the structure variable can be defined:

struct Structure type name structure variable name;

  
  • 1
struct student stu1,stu2;//Define two structure variables

  
  • 1

You can also define structure variables when defining structure types, such as:

struct student
{
	int num;
	char name[10];
	int computer,english,math;
	double average;
}stu1,stu2;//Define two structure variables;

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

You can also omit the structure type name, such as:

struct//The structure type name student is omitted
{
	int num;
	char name[10];
	int computer,english,math;
	double average;
}stu1,stu2;//Define two structure variables;

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

The name of the structure type is omitted. In this case, the structure variable can only be defined later, not in the main function. Therefore, it is not recommended to define structure variables in this way

  • Note: the variable name and member name of structure variable can be the same, and they do not affect or interfere with each other, such as:
struct date
{
	int year;
	int month;
	int day;
};
int main()
{
	struct date year;
	year.year = 1980;
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Use of structure variables

The use form is: structure variable. Structure member name

scanf("%d%s%d%d%d",&s1.num,s1.name,&s1.math,&s1.english,&s1.computer);

  
  • 1
  • Note: if there are character array members in the structure type, and you want to assign a string constant to the character array in the main function, you cannot directly use "=", that is, s1.name = "Zhang San"; It is an error. You must use the string copy function strcpy() function to implement it, such as strcpy(s1.name, "Zhang San");

  • Values can be assigned between structural variables of the same type
    For example: stu2=stu1; Assign the values of all member variables in the structure variable stu1 to the structure variable stu2 respectively

Structure variables as function parameters

  • Members of structural variables are used as arguments to functions, and formal parameters are ordinary variables or arrays
  • Structure variables can also be used as parameters of functions
    For example: (see the detailed comments in the code and observe the running results)
#include <stdio.h>
#include <string.h>
struct s_score
{
	int no;
	char name[10];
	int score[3];
};//Don't forget the semicolon
void output(struct s_score a);//Formal parameters are structural variables
void fun(int *q);//Formal parameters are pointers 
main()
{
	struct s_score a={1001,"zhangsan",{60,60,60}};//This line of statement initializes the structure variable
	output(a);
	a.no=1001;
	strcpy(a.name,"wang lin");
	a.score[0]=78;
	a.score[1]=88;
	a.score[2]=94;
	output(a);
	fun(a.score);//The array member in the structure. The array name is the first address of the array 
	output(a);
}
void output(struct s_score a)//Receive the structure variable from the main function as a formal parameter 
{
	int i;
	printf("%d  %s   ",a.no,a.name);
	for(i=0;i<3;i++)
		printf("%4d",a.score[i]);
	printf("\n");
}
void fun(int *p)//Defines a pointer variable to receive the first address of the array passed in by the main function 
{
	int i;
	for(i=0;i<3;i++)
	{
		*(p+i)+=10;//The pointer variable is operated on through the loop variable i 
		if(*(p+i)>100)
			*(p+i)=100;
	}
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

Initialization of structure variables

The general initialization form of structure variables is:
Structure variable name = {initial value table};
Like the above statement:

struct s_score a={1001,"zhangsan",{60,60,60}};

  
  • 1

The data type of the initial value of the structure should be consistent with that required by the corresponding member in the structure variable, otherwise an error will occur

Posted by OMorchoe on Sat, 02 Oct 2021 13:06:32 -0700