C 使用结构时获取不兼容的指针类型错误
我正在写一个程序,在这个程序中,我必须创建一个员工列表,并处理其中的信息。在C 使用结构时获取不兼容的指针类型错误,c,pointers,struct,C,Pointers,Struct,我正在写一个程序,在这个程序中,我必须创建一个员工列表,并处理其中的信息。在writeToFile中,我写入信息。到.txt文件。在newList中,我应该创建一个新列表,其中包含符合特定标准的员工。但是,当我初始化*newListHead指针时,我从不兼容的指针类型列表*{aka'struct newEmployeeList*}[-Wincompatible指针类型]中得到初始化'newEmployeeList*{aka'struct newEmployeeList*}错误。既然我对头指针做了
writeToFile
中,我写入信息。到.txt
文件。在newList
中,我应该创建一个新列表,其中包含符合特定标准的员工。但是,当我初始化*newListHead
指针时,我从不兼容的指针类型列表*{aka'struct newEmployeeList*}[-Wincompatible指针类型]中得到初始化'newEmployeeList*{aka'struct newEmployeeList*}错误。既然我对头指针做了相同的操作,那么我做错了什么
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\u new\u node
函数返回一个List*
,但您正在将该值分配给一个newEmployeeList*
。这些类型不兼容
将newListHead
的类型更改为List*
,或者创建一个不同的函数,返回newEmployeeList*
的新实例。我建议使用前者,因为没有理由根据您所展示的内容使用类型newEmployeeList
。为什么您有一个单独的List
和newEmployeeList
除了名称之外完全相同?List
和newEmployeeList
是两种不同的类型。两种类型都应该只有一种类型。只要去掉newEmployeeList
类型并使用List
替换它。我必须创建一个新的结构,因为这是教授的要求。我不能改变它@AKX@zaro新类型的结构,或现有结构的新实例?@dbush new list,将只包含某些employeesThank。没有新员工名单的原因是因为这是教授的要求。