Pregunta

Editar: El código siguiente se ha modificado para que funcione a medida que se soluciona el problema.

Específicamente, (*hardwareList.next_item)->next se escribió originalmente sin corchetes (por ejemplo, como *hardwareList.next_item->next) y el compilador no lo entendió.

Estoy tratando de averiguar por qué el compilador se confunde con mi código C.Estoy tratando de crear una lista vinculada que almacene todos los elementos y también un puntero a la dirección de la última variable "siguiente", para agregar fácilmente.

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

El compilador dice:

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
¿Fue útil?

Solución

Supongo que el problema es este fragmento de código: *(hardwareList.next_item)->data

next_item es un puntero a un puntero, así que supongo que el compilador lee esto como *((hardwareList.next_item)->data) que, por supuesto, no funciona; los punteros no tienen ningún miembro en C.

Pruebe ((*(hardwareList.next_item))->data) para obtener el orden correcto de desreferencia.

Otros consejos

hardwareList.next_item es HardwareListItem**, por lo que el operador -> en ella devuelve HardwareListItem*, lo que obviamente no es una estructura.

Estás usando demasiados punteros, es confuso.Trate de simplificar su código, tiene toneladas de errores allí.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top