Introduction to C language 8: introduction to structure

1. What is the structure?

Array is a set of data with the same order and type. What if we want to put several different types of data together?
Arrays are multiple variables of the same type bound together; Structures are multiple variables of different types tied together.

For example, the name, age and grade of a student are composed of different data types, that is, the name is string data, the age is integer data, and the grade is floating point (decimal) data. At this time, we can use the structure to implement it.

2. How to use the structure?

2.1 define structure

(1) Grammar

struct Structure name{
    Member list;
};

Members in a structure are defined in the same way as variables, that is, multiple variables are defined in the structure.
For example, define a student's structure:

struct Student {
    char  name[32];   //full name
    int  age;     //Age
    float  score;   //achievement
};

2.2 defining structure variables

struct  Student student1;

The above defines a structural variable student1 of Student type, which can represent a Student. He has three members: name, age and grade.

2.3 structure member reference

The structure cannot input and output the whole, and the members need to be operated separately, which is called structure variable member reference.
The format is as follows:

Structure variable name.member name

There is an English period between the variable name and member of the structure.
For example: input and output student information

#include <stdio.h>

struct Student {
    char  name[32];   //full name
    int  age;     //Age
    float  score;   //achievement
};

int main(){
    struct Student student1;
    scanf("%s%d%f",&student1.name,&student1.age,&student1.score);
    printf("full name:%s\n Age:%d\n fraction:%0.1f\n",student1.name,student1.age,student1.score);
}

2.4 assignment of structure members

Structure variable member assignment is to assign values to all members in the structure in turn.

struct Student student1;
struct Student student2;

strcpy(student1.name,"Zhang San"); 
student1.age = 19;
student1.score = 90.5;

strcpy(student2.name,"Li Si");
student2.age = 18;
student2.score = 95.5;

printf("full name\t Age\t fraction\n");
printf("%s\t%d\t%.1f\n",student1.name,student1.age,student1.score);
printf("%s\t%d\t%.1f\n",student2.name,student2.age,student2.score);

Note: numeric type members can be assigned directly, and string type variables need to use string copy function.

2.5 structure assignment

The overall assignment of structure variables is to assign values to all members of the structure.

struct Student student1;
struct Student student2;

strcpy(student1.name,"Zhang San"); 
student1.age = 19;
student1.score = 90.5;

student2 = student1;

printf("full name\t Age\t fraction\n");
printf("%s\t%d\t%.1f\n",student1.name,student1.age,student1.score);
printf("%s\t%d\t%.1f\n",student2.name,student2.age,student2.score);

The structure can be assigned as a whole, and the array cannot be assigned directly.

2.6 overall initialization of structure

If the structure variable is initialized directly, the operation is simpler.

struct Student student1 = {"Zhang San",19,90.5};
struct Student student2 = {"Li Si",18,95.5};

printf("full name\t Age\t fraction\n");
printf("%s\t%d\t%.1f\n",student1.name,student1.age,student1.score);
printf("%s\t%d\t%.1f\n",student2.name,student2.age,student2.score);

Note: the order of assignment data must be consistent with that of structure member declaration.

2.7 initialization of structure

In addition to the overall initialization, the structure can also be initialized like the following parts.

struct Point3D{
    int x;
    int y;
    int z;  
};
struct Point3D p = {.x=10,.z=20}; 

3. Other grammar

3.1 define structure and structure variables at the same time

struct Student{
    char  name[32]; //full name
    int  age; //Age
    float  score; //achievement
} student1,student2;

3.2 define structure and structure variables and assign initial values

struct Student{
    char  name[32]; //full name
    int  age; //Age
    float  score; //achievement
} student1 = {"Zhang", 19, 90.5};

Keywords: C Back-end

Added by shatztal on Wed, 27 Oct 2021 04:57:00 +0300