编译错误:请求成员不是结构或联合

前端之家收集整理的这篇文章主要介绍了编译错误:请求成员不是结构或联合前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑:下面的代码已被修改为工作,因为问题已解决.

具体来说,(* hardwareList.next_item) – > next最初是在没有括号的情况下编写的(例如,作为* hardwareList.next_item-> next)并且编译器不理解它.

我正在努力解决为什么编译器与我的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;
}

编译说:

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

解决方法

我的猜测问题是这段代码:*(hardwareList.next_item) – >数据

next_item是指向指针的指针,所以我的猜测是编译器将其读作*((hardwareList.next_item) – > data)当然不起作用 – 指针在C中没有任何成员.

尝试((*(hardwareList.next_item)) – > data)以获得正确的解引用顺序.

猜你在找的C&C++相关文章