Initialization and reference of structure variables

Initialization and reference of structure variables

mission details

The task of this level: input the student ID, name and grade (integer) of two students from the keyboard, and store them in the structure respectively, and output the student ID, name and grade of the student with higher grade.

related information

The structure type is used to describe the composite data composed of multiple data of different data types, and is a user-defined data type.

Definition of structure

The definition format is

struct <结构体类型名>
{
    
    
    <成员表>
};  //分号结束定义

For example, the following is a definition of a structure type describing student information:

struct student
{
    
    
    long number;
    char name[20];
    char sex;
    float score;
};

In this structure definition, the structure type is named student, and the structure consists of 4 members.
The first member is number, an integer variable; the
second member is name, a character array; the
third member is sex, a character variable; the
fourth member is score, a real variable.
It should be noted that the semicolon after the curly braces is essential.

Declare structure variables

There are four ways to declare structure variables. They are essentially the same. You can choose the way you like according to your habits and requirements:

  1. Define the structure first, then declare the structure variable
struct student
{
    
    
  long number;
  char name[20];
  char sex;
  float score;
};    //先定义结构体
struct student s1,s2;//声明结构体变量
  1. Declare the final variable while defining the structure type
struct student
{
    
    
  long number;
  char name[20];
  char sex;
  float score;
}s1,s2;    //在定义结构体的同时定义结构体变量
  1. Declare structure variables directly
struct
{
    
        //省去结构体名
  long number;
  char name[20];
  char sex;
  float score;
}s1,s2;    //直接给出结构体变量
  1. typedef refers to aliases to define
typedef struct student STUDENT;     //给结构体student定义别名
struct student
{
    
    
  long number;
  char name[20];
  char sex;
  float score;
};
STUDENT s1,s2;    //使用别名STUDENT定义结构体

Structure member access

The member access methods of structure variables are:

<structure variable name>.<structure member variable>

Each member of a structure variable can be regarded as an independent variable, called a member variable. The operations that can be done on a member variable are determined by the type of the member variable.

For example, the following operation is legal

s1.number=2010001;//number成员是long类型
strcpy(s1.name,"lili")//name成员是字符数组类型

Structure members should be used in conjunction with structure variable names, that is, in the access mode of "structure variable name. member name", so the member names of different structure types can be the same, and they can also be used with other non-structures in the program The names of the members are the same and will not cause ambiguity or conflict.

Note:
If the member of the structure variable itself is a structure type, several member operators must be used to find the lowest-level member, and only the lowest-level member can be assigned or read and written.

E.g:

struct data
{
    
    
    int month;
    int day;
    int year;
};
struct student
{
    
    
    long num;
    char name[20];
    char sex;
    data birthday;
}stu1;

For the structure variable stu1 defined above, the members can be accessed in the following ways:

stu1.num;
stu1.birthday.month;

Structure variable initialization

In the definition of the structure while the variable may be made thereto initialized , the array format is similar variable initialization, it braces the initial value of each member is enclosed, each of the initial value corresponding to the respective members.

For example, to initialize studena variable of structure type s1:

struct student s1={
    
    2010001,"lili",'F',97};

When defining a structure type, its members cannot be initialized, because the type is not an entity of the program at runtime, and memory space will not be allocated to them. Therefore, its initialization is meaningless.

Programming requirements

Supplement the code, save the test input information into the structure, and complete the output of the student ID, name and grade of the student with a higher score.

test introduction

The platform will test the code you write and compare the value you output with the actual correct value. Only when all the data are calculated correctly can the test pass:

Test input:

14308101 xiaowang 99
15408122 xiaoli 78

Expected output:

14308101 xiaowang 99

Test input data description:

The input consists of two lines, each line contains the student ID (8-digit integer), name (string, length not exceeding 20) and grade (integer).

code show as below

#include<stdio.h>
#include<stdlib.h>
int main()
{
    
    
	struct  student
	{
    
    
		int num;
		char name[20];
		int score;
	}student1,student2;
  scanf("%d%s%d",&student1.num,student1.name,&student1.score);
  scanf("%d%s%d",&student2.num,student2.name,&student2.score);
  if(student1.score>student2.score)
  printf("%d %s %d\n",student1.num,student1.name,student1.score);
  else  if(student1.score<student2.score) 
  printf("%d %s %d\n",student2.num,student2.name,student2.score);
  else
  {
    
    printf("%d %s %d\n",student1.num,student1.name,student1.score);
   printf("%d %s %d\n",student2.num,student2.name,student2.score);
  }
  return 0;
}

Guess you like

Origin blog.csdn.net/weixin_51705589/article/details/112968592