编辑:下面的代码已经被修改,并且问题已经解决。
具体而言,(*hardwareList.next_item)->next
最初编写没有括号(例如,*hardwareList.next_item->next
)并且编译器无法理解它。
我正在尝试弄清楚为什么编译器会困惑于我的C代码。我正在尝试创建一个链接列表,用于存储所有项以及指向最后“next”变量地址的指针,以便轻松附加。
typedef struct {
int recordNum;
char toolName[25];
int quantity;
float cost;
} HardwareData;
typedef struct _HardwareListItem{
HardwareData data;
struct _HardwareListItem* next;
} HardwareListItem;
typedef struct _HardwareList {
HardwareListItem* items;
HardwareListItem** next_item;
} HardwareList;
HardwareList readFromFile(FILE* fp)
{
char stopReading = 0;
HardwareList hardwareList = {0};
hardwareList.next_item = &hardwareList.items;
do {
*hardwareList.next_item = (HardwareListItem*)calloc(1, sizeof(HardwareData));
if (*hardwareList.next_item == NULL)
{
fprintf(stderr, "OOM Reading File\n");
fflush(stderr);
exit(EXIT_FAILURE);
}
if (fread(&((*hardwareList.next_item)->data), sizeof(HardwareData), 1, fp) != 1) {
free(*hardwareList.next_item);
*hardwareList.next_item = NULL;
stopReading = 1;
} else {
hardwareList.next_item = &((*hardwareList.next_item)->next);
}
} while(!stopReading);
return hardwareList;
}
编译器说:
line 31: error: request for member 'data' in something not a structure or union
line 36: error: request for member 'next' in something not a structure or union