繁体   English   中英

指向结构中指针的指针数组

[英]Array of pointers to a struct with pointers in that struct

困扰我的部分是最后一个for循环,我用它来测试数据输入是否正确以及是否正确使用printf打印。 用于打印我输入的数据的三种访问方法对我来说并不十分清楚。

访问方法#1中,我设法仅使用一个箭头操作符来正确打印数据以访问名称。 我无法理解的部分是为什么我能够在没有错误的情况下访问数据? 我只使用索引来访问每个production_plant_employees结构。 我知道括号进行解除引用,但我仍然不明白那里发生了什么。 我尝试写这样的部分: *(production_plant_employees + i) ,但它不起作用。

访问方法#2对我来说完全清楚。

现在访问方法#3 ,这是我假设的那个可行,但它拒绝。 编写时,IDE显示没有错误,但是当我运行程序时,它会停止。

我应该首先访问第一个指针(即production_plant_employees )中的数据,然后访问第二个指针中的数据(这是指向struct employee指针basic_info ),然后,当我经历了2个指针时,访问我追求的数据(姓名,年龄等),对吗?

另外,您能否告诉我任何其他可能的方式来访问我之后的数据?

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);

首先我们首先解除引用指针production_plant_employees +i ,现在,我们访问成员basic_info ,它也是一个指针,需要使用第二个*来取消引用以访问本地成员name

ptr1 = production_plant_employees +i
ptr2 = (*ptr1).basic_info
data = (*ptr2).name

因此(在data中用ptr2代替:

data = (*(*ptr1).basic_info).name

最后用ptr1代替:

data = (*(*(production_plant_employees +i)).basic_info).name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM