问题描述
编辑: 的code以下已被修改,这个问题已经得到解决工作的
的具体来说,(* hardwareList.next_item) - >接下来
最初编写不带括号(例如,作为 * hardwareList.next_item- >接着
)和编译器不明白的
我想锻炼为什么编译器是越来越有我的C code混淆。我试图创建一个存储的所有项目,也是一个指针地址的最后下一个变量,简单的追加一个链表。
typedef结构{
INT recordNum;
字符工具名[25];
INT数量;
浮动费用;
} HardwareData;typedef结构_HardwareListItem {
HardwareData数据;
结构_HardwareListItem *接下来的;
} HardwareListItem;typedef结构_HardwareList {
HardwareListItem *物品;
HardwareListItem ** NEXT_ITEM;
} HardwareList;HardwareList readFromFile(FILE * FP)
{
焦炭STO preading = 0;
HardwareList hardwareList = {0};
hardwareList.next_item =安培; hardwareList.items;
做{
* hardwareList.next_item =(HardwareListItem *)释放calloc(1,sizeof的(HardwareData));
如果(* hardwareList.next_item == NULL)
{
fprintf中(标准错误,OOM阅读文件\\ n);
fflush(标准错误);
出口(EXIT_FAILURE);
}
如果(的fread(及((* hardwareList.next_item) - >!数据),的sizeof(HardwareData),1,FP)= 1){
免费(* hardwareList.next_item);
* hardwareList.next_item = NULL;
STO preading = 1;
}其他{
hardwareList.next_item =及((* hardwareList.next_item) - >接着);
}
}而(STO preading!); 返回hardwareList;
}
编译器说:
第31行:错误:请求的东西不是一个结构或联合成员数据
第36行:错误:没有东西结构或联合要求会员'下一个'
我的猜测,问题是这片code的: *(hardwareList.next_item) - GT;数据
NEXT_ITEM
是一个指针的指针,所以我的猜测是,编译器读取此为 *((hardwareList.next_item) - GT;数据)
这当然行不通 - 指针没有任何成员用C
尝试((*(hardwareList.next_item)) - GT;数据)。
来得到正确的解引用顺序
Edit: The code below has been modified to work as the problem has been solved.
Specifically, (*hardwareList.next_item)->next
was originally written without brackets (e.g. as *hardwareList.next_item->next
) and the compiler didn't understand it.
I'm trying to workout why the compiler is getting confused with my C code. I'm trying to create a linked list that stores all the items and also a pointer to the address-of the last "next" variable, for easy appending.
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;
}
Compiler says:
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
My guess the problem is this piece of code: *(hardwareList.next_item)->data
next_item
is a pointer to a pointer, so my guess is that the compiler reads this as *((hardwareList.next_item)->data)
which of course doesn't work - pointers don't have any members in C.
Try ((*(hardwareList.next_item))->data)
to get the correct dereference order.
这篇关于编译错误:没有东西结构或联合申请为会员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!