C语言 编译错误:请求不是结构或联合的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7384581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
compilation error: request for member in something not a structure or union
提问by Adam M-W
Edit:The code below has been modified to work as the problem has been solved.
编辑:下面的代码已被修改为可以工作,因为问题已解决。
Specifically, (*hardwareList.next_item)->nextwas originally written without brackets (e.g. as *hardwareList.next_item->next) and the compiler didn't understand it.
具体来说,(*hardwareList.next_item)->next最初编写时没有括号(例如 as *hardwareList.next_item->next)并且编译器不理解它。
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.
我试图弄清楚为什么编译器会与我的 C 代码混淆。我正在尝试创建一个链接列表,该列表存储所有项目以及指向最后一个“下一个”变量地址的指针,以便于追加。
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
回答by Aaron Digulla
My guess the problem is this piece of code: *(hardwareList.next_item)->data
我猜问题是这段代码: *(hardwareList.next_item)->data
next_itemis 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.
next_item是一个指向指针的指针,所以我的猜测是编译器将*((hardwareList.next_item)->data)其读取为这当然不起作用 - 指针在 C 中没有任何成员。
Try ((*(hardwareList.next_item))->data)to get the correct dereference order.
尝试((*(hardwareList.next_item))->data)获得正确的取消引用顺序。
回答by littleadv
hardwareList.next_itemis HardwareListItem**, so operator ->on it returns HardwareListItem*, which obviously is not a struct.
hardwareList.next_itemis HardwareListItem**,所以->它的运算符返回HardwareListItem*,这显然不是一个结构体。
You're using too many pointers, it is confusing. Try to simplify your code, you have tons of bugs there.
你使用了太多的指针,这很令人困惑。尝试简化您的代码,那里有很多错误。

