compilation error: request for member in something not a structure or union
Asked Answered
M

2

7

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
Mariannamarianne answered 12/9, 2011 at 7:28 Comment(6)
The compiler doesn't give line numbers? Bad compiler...Autosuggestion
(hardwareList.next_item)->next won't work as next_item is not a ptr to a structCarolanncarole
@QuentinUK: Whoops, my bad. I'll edit in a sec.Mariannamarianne
@Aaron-Digulla: Compiler did give line numbers but they wouldn't have matched up with what I copied in.. Edited now with new line numbers.Mariannamarianne
In that case add a comment to the pasted code like which contains the error message :-)Autosuggestion
https://mcmap.net/q/223022/-what-does-quot-request-for-member-39-39-in-something-not-a-structure-or-union-quot-mean/1111160Disinter
A
8

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.

Autosuggestion answered 12/9, 2011 at 7:35 Comment(1)
Thanks heaps, this has been driving me insane thinking about how I've got it wrong.Mariannamarianne
A
4

hardwareList.next_item is HardwareListItem**, so operator -> on it returns HardwareListItem*, which obviously is not a struct.

You're using too many pointers, it is confusing. Try to simplify your code, you have tons of bugs there.

Arezzo answered 12/9, 2011 at 7:33 Comment(1)
I've tried to simplify (see updated code above), but still pretty confusing.Mariannamarianne

© 2022 - 2024 — McMap. All rights reserved.