Compilation error: query for member in something not structure or union

Edit: The code below has been modified to work as the problem has been resolved.

In particular, (*hardwareList.next_item)->next was originally written without brackets (for example, as *hardwareList.next_item->next ), and the compiler did not understand this.

I am trying to train why the compiler gets confused with my C code. I am trying to create a linked list that stores all the items, as well as a pointer to the address of the last variable "next" for easy addition.

 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; } 

The 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 
+7
source share
2 answers

I think the problem is this piece of code: *(hardwareList.next_item)->data

next_item is a pointer to a pointer, so I assume that the compiler reads it as *((hardwareList.next_item)->data) , which of course does not work, pointers do not have any members in C.

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

+7
source

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

You use too many pointers, this is confusing. Try to simplify your code, you have a lot of errors there.

+4
source

All Articles