簡體   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