[C language] structure pointer and structure array

table of Contents

One, structure pointer

Two, structure array

1. Definition of structure array

2. Initialization of the structure array

3. Reference to structure array

4. Structure array pointer


One, structure pointer

Similar to general pointers, structures can also be referenced using structure pointers. The definition of structure pointer is as follows:

struct Student stu = {1001,"Li Lei",'M',1.87 }; //Define a Student's structure variable stu

struct Student *p=&stu; //Define a Student structure pointer variable to point to stu

When a pointer to a structure variable is defined in the program, the members of the structure variable can be accessed through " pointer name -> member variable name "

Example demonstration:

#include <stdio.h>

typedef char NAME[10];
typedef double DOU;
typedef struct Student
{
    int num;
    NAME name;
    char sex;
    DOU height;

}Stu;


int main() {
    Stu stu = { 1001,"Li Lei",'M',1.87 };
    Stu* p = &stu;
    printf("学生信息:\n");
    printf("学号:%d\n", p->num);
    printf("姓名:%s\n", p->name);
    printf("性别:%c\n", p->sex);
    printf("年龄:%.2lf\n", p->height);
}

operation result:


Two, structure array

1. Definition of structure array

A structure variable can only store a set of information. If you want to store a large amount of information, you can define a structure array to store it.

There are three ways to define structure arrays:

① Define the structure type first, and then define the structure array

struct Student
{
    int num;
    char name[10];
    char sex;
    double height;

};

struct Student stus[20];

②Define the structure array while defining the structure type

struct Student
{
    int num;
    char name[10];
    char sex;
    double height;

}stus[20];

③Define the structure array directly

struct
{
    int num;
    char name[10];
    char sex;
    double height;

}stus[20];

2. Initialization of the structure array

Structure arrays are similar to general arrays, all of which are initialized by assigning values ​​to elements. Since each element in the structure array is a structure variable, when assigning a value to each element, you need to put the value of its member in a pair of braces in turn.

The initialization method of the structure array:

① Define the structure type first, then define and initialize the structure array

struct Student
{
    int num;
    char name[10];
    char sex;
    double height;

};

struct Student stu1[3] = { {1001,"Li Lei",'M',1.78},
                            {1002,"Li Hua",'M',1.87},
                            {1003,"Han Mei",'W',1.65}
                          };

②Define and initialize the structure array while defining the structure type

struct Student
{
    int num;
    char name[10];
    char sex;
    double height;

}stu1[3] = { {1001,"Li Lei",'M',1.78},
             {1002,"Li Hua",'M',1.87},
             {1003,"Han Mei",'W',1.65}
            };

When you initialize the structure array in this way, you do not need to specify the length of the structure array. The system will automatically determine the length of the structure array according to the initialized value when compiling.

3. Reference to structure array

The reference of a structure array refers to a reference to a structure array element . Since each structure array element is a structure variable, the reference of a structure array element is similar to a structure variable.

Example demonstration:

#include <stdio.h>

struct Student
{
    int num;
    char name[10];
    char sex;
    double height;

};

struct Student stus[3] = { {1001,"Li Lei",'M',1.78},
                            {1002,"Li Hua",'M',1.87},
                            {1003,"Han Mei",'W',1.65}

                            };

int main() {
    for (int i = 0; i < 3; i++) {
        printf("学生%d的信息\n", i + 1);
        printf("学号:%d\n", stus[i].num);
        printf("姓名:%s\n", stus[i].name);
        printf("性别:%c\n", stus[i].sex);
        printf("身高:%.2lf\n", stus[i].height);
        printf("\n");
    }
   
}

operation result:

4. Structure array pointer

The pointer can point to the structure array, that is, assign the starting address of the structure array to the pointer variable. This kind of pointer is the structure array pointer.

Example demonstration:

#include <stdio.h>

struct Student
{
    int num;
    char name[10];
    char sex;
    double height;

};

struct Student stus[3] = { {1001,"Li Lei",'M',1.78},
                            {1002,"Li Hua",'M',1.87},
                            {1003,"Han Mei",'W',1.65}

                            };

int main() {
    struct Student* p=stus ;//定义结构体数组指针p指向结构体数组stus
   
    for (int i = 0; i < 3; i++) {
        printf("学生%d的信息\n", i + 1);
        printf("学号:%d\n", p->num);
        printf("姓名:%s\n", p->name);
        printf("性别:%c\n", p->sex);
        printf("身高:%.2lf\n", p->height);
        printf("\n");
        p++;
    }

}

operation result:

From the point of view of definition, the structure array pointer is no different from the structure pointer, except that it points to the structure array.

 

 

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/109203485