0

I cannot manage to solve the error, even though I've used these types of structures in other project in the same manner and it worked.

Meanwhile I'm not getting an errors in the declaration or initialisation of my structures.

The idea is to read information about a bunch of cars from a binary file. It tells me that "struct Car" is an incomplete type and not allowed. Here's the code I've made so far:

car.c

#include <stdio.h>
#include <string.h>

#include "car.h"

#define MAX_NUMBER_OF_CARS 10
struct Car importedCars[MAX_NUMBER_OF_CARS];
struct Config importedConfigs[MAX_NUMBER_OF_CARS];

int main( int argc, char* argv[]){
                       
    FILE *fp;
    fp = fopen("cars.bin","rb");
    if(fp != NULL)
    {
        fread(importedCars,sizeof(struct Car),MAX_NUMBER_OF_CARS,fp);
        printf("1. Binary File read, structures of cars created.\n\n");
        for(int i = 0; i < MAX_NUMBER_OF_CARS; i++)
        {
            printf("%s\t%s\t%s\t%d\t%f\t%f\t%f\t%d\n", importedCars[i].make, importedCars[i].model, importedCars[i].configuration.fuelType, importedCars[i].power, importedCars[i].torque, importedCars[i].fuelConsumption, importedCars[i].co2PerKm, importedCars[i].price);
        }
    }
    else
    {
        printf("cannot read file.\n");
        
    } 
    fclose(fp);
    return 0;
}

car.h

#ifndef _CAR_H_
#define _CAR_H_

#define fuelLength 10
#define gearLength 10
#define conditionLength 10
#define makeLength 30
#define modelLength 30

typedef struct {
    char fuelType[fuelLength];
    char gearType[gearLength];
    int numGears;
    char condition[conditionLength];
} Config;

typedef struct {
    char make[makeLength];
    char model[modelLength];
    Config configuration;
    short power;
    float torque;
    float fuelConsumption;
    float co2PerKm;
    int price;
} Car;
mr. yoxon
  • 13
  • 3
  • 2
    You don't have a type `struct Car`, you have a type `Car`. Same with `Config`. Please open up the register of your text-book and find out the sections about `typedef` to read about it. – Some programmer dude Dec 03 '22 at 13:51
  • @Someprogrammerdude thank you for answering so quickly. i think i managed to resolve the problem. though there are other errors coming up now. since im not learning from a textbook and more through "learning by doing", do you have any good source to read a bit more about typedefs? – mr. yoxon Dec 03 '22 at 14:14
  • [Here's a random list of books about C](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). Most of them should be good enough to learn from. And I really recommend you invest in a couple of books, "learning by doing" leads to a *lot* of trial-and-error, with mostly errors. – Some programmer dude Dec 03 '22 at 14:20

0 Answers0