簡體   English   中英

使用結構時出現不兼容的指針類型錯誤

[英]Getting incompatible pointer type error while working with structs

我正在編寫一個程序,我必須在其中創建一個員工列表並使用其中的信息。 writeToFile我寫信息。 .txt文件。 newList中,我應該創建一個新列表,其中將包含符合某些條件的員工。 但是,當我初始化*newListHead指針時,我會initialization of 'newEmployeeList *' {aka 'struct newEmployeeList *'} from incompatible pointer type 'List *' {aka 'struct List *'} [-Wincompatible-pointer-types]| 錯誤。 我做錯了什么,因為我head指針做了同樣的事情?

typedef struct employee {
    char name[20];
    double salary;
    char gender[10];
} employee;

typedef struct List {
    employee info;
    struct List *next;
} List;

typedef struct newEmployeeList {
    employee info;
    struct newEmployeeList *next;
} newEmployeeList;

List *create_new_node() {
    List *new_node = NULL;
    new_node = (List*)malloc(sizeof(List));
    new_node->next = NULL;
    return new_node;
};

int main()
{
    List *head = create_new_node();
    writeToFile("test.txt", head);

    newEmployeeList *newListHead = create_new_node();
    newList(head, newListHead);

    return 0;
}

正如錯誤消息所說, create_new_node function 正在返回List *但您將該值分配給newEmployeeList * 這些類型是不兼容的。

newListHead的類型更改為List *或創建一個不同的 function 以返回newEmployeeList *的新實例。 我推薦前者,因為甚至沒有理由從您所顯示的內容中獲得類型newEmployeeList

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM