c - 指向该结构的指针的数组

困扰我的部分是最后一个for循环,我用它来测试数据输入是否正确,以及它是否使用printf正确打印。用于打印我输入的数据的三种访问方法对我来说不太清楚。
在access method#1中,我仅使用一个箭头运算符来访问名称,成功地打印了数据。为什么我能够访问数据而不出错,这是我无法理解的部分?我只使用索引来访问每个production_plant_employees结构。我知道括号可以解引用,但我还是不明白发生了什么。我试着这样写那部分:*(production_plant_employees + i),但没用。
我完全清楚访问方法2。
现在,访问方法3,这是我认为可行的方法,但它拒绝这样做。编写时,IDE不会显示错误,但当我运行程序时,它会停止。
我应该先访问第一个指针中的数据(这是production_plant_employees),然后访问第二个指针中的数据(这是指针basic_info,这是结构employee),然后,当我完成这两个指针后,访问我要访问的数据(名称、年龄等),对吗?
另外,你能告诉我其他可能的方法来访问我要的数据吗?

typedef struct basicdata{
    char name[15];
    char last_name[15];
    char gender[2];
    int age;
    char birthplace[15];
    char address[15];
} BASICDATA;

typedef struct job_info {
    int employment_year;
    char job_position[20];
    char employee_pay_grade[10];
    int employee_grade;
} JOB_INFO;

typedef struct employee{
    BASICDATA *basic_info;
    JOB_INFO *job_info;
} EMPLOYEE;


int main () {

    int i;
    int choice = 0;

    EMPLOYEE *production_plant_employees;

    printf("Enter number of employees : \n");
    scanf("%d", &choice);

    production_plant_employees = (EMPLOYEE*)calloc(choice, sizeof(EMPLOYEE));
    if (production_plant_employees == NULL) {
        printf("An error occured during memory allocation\n");
    }

    for(i = 0; i < choice; ++i) {
        production_plant_employees[i].basic_info = (BASICDATA*)calloc(choice, sizeof(BASICDATA));
        if(production_plant_employees[i].basic_info == NULL) {
            printf("An error occured during memory allocation\n");
        }

        production_plant_employees[i].job_info = (JOB_INFO*)calloc(choice, sizeof(JOB_INFO));
        if(production_plant_employees[i].job_info == NULL) {
            printf("An error occured during memory allocation\n");
        }

        printf("production_plant_employees[%d].basic_info = %d\t%x\n", i, production_plant_employees[i].basic_info, production_plant_employees[i].basic_info);
        printf("production_plant_employees[%d].job_info = %d\t%x\n", i, production_plant_employees[i].job_info, production_plant_employees[i].job_info);
    }

    for(i = 0; i < choice; ++i) {
        fflush(stdin);
        printf("Enter name : \n");
        fgets(production_plant_employees[i].basic_info->name, 15, stdin);

        printf("Name of %d. employee : %s", i, production_plant_employees[i].basic_info->name) //access method#1
        printf("Name of %d. employee : %s", i, (production_plant_employees + i)->basic_info->name);  //access method #2
        printf("Name of %d. employee : %s", i, *(*(production_plant_employees +i)).basic_info->name); //access method #3 ---> why isn't this working?
        printf("\n\n");
    }
    return 0;
}

最佳答案

正确的方法是(对于访问方法3):

printf("Name of %d. employee : %s", i, (*(*(production_plant_employees +i)).basic_info).name);

首先,我们从解引用指针开始,现在,我们访问member production_plant_employees +i,member basic_info也是指针,需要使用第二个 *来解引用以访问本地成员。
ptr1 = production_plant_employees +i
ptr2 = (*ptr1).basic_info
data = (*ptr2).name

因此(在 name中替换 ptr2
data = (*(*ptr1).basic_info).name

&最后通过替换 data
data = (*(*(production_plant_employees +i)).basic_info).name