编译错误:请求不是结构或联合的成员 [英] compilation error: request for member in something not a structure or union

查看:19
本文介绍了编译错误:请求不是结构或联合的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码已被修改,问题已解决.

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

Specifically, (*hardwareList.next_item)->next was originally written without brackets (e.g. as *hardwareList.next_item->next) and the compiler didn't understand it.

我试图弄清楚为什么编译器会与我的 C 代码混淆.我正在尝试创建一个链接列表,该列表存储所有项目以及指向最后一个下一个"变量地址的指针,以便于追加.

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
");
      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)->data

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

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.

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

Try ((*(hardwareList.next_item))->data) to get the correct dereference order.

这篇关于编译错误:请求不是结构或联合的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆