I'm trying to write a function that reads a file into an array of structs and returns said array. When I do test runs, it seems to be working appropriately (i.e. prints each entry as anticipated). But I keep getting a warning:
main.c:53:16: warning: incompatible pointer types returning 'struct Vehicles *' from a
function with result type 'struct Vehicles *' [-Wincompatible-pointer-types]
return inventory;
Which sounds a little funny because, come on, struct Vehicles * is incompatible with struct Vehicles *? Can anyone help me understand why I am getting this warning and possibly provide additional insight into how to appropriately return an array of structs?
The 'hw2.data' file I am testing this with only has three entries (but our instructor will test with 100 entries) and looks like this:
F150 5.4 28000 white
RAM1500 5.7 32000 orange
TOYOTA 2.1 16000 red
And my function (so far), looks like this:
struct Vehicles *readFile(char file_name[16]) {
struct Vehicles {
char vehicle[16];
float engine;
int price;
char color[16];
};
struct Vehicles *inventory = malloc(sizeof(struct Vehicles)*100);
FILE *input;
char vehicle[16];
float engine;
int price;
char color[16];
int count = 0;
//struct Vehicles inventory[3];
input = fopen(file_name, "r");
while (fscanf(input, "%s %f %d %s", vehicle, &engine, &price, color) == 4) {
strcpy(inventory[count].vehicle, vehicle);
strcpy(inventory[count].color,color);
inventory[count].engine = engine;
inventory[count].price = price;
printf("%s %.2f %d %s\n", inventory[count].vehicle, inventory[count].engine, inventory[count].price, inventory[count].color);
count++;
}
fclose(input);
return inventory;
}
int main(void) {
readFile("hw2.data");
return 0;
};
Vehiclesinside the function, which means it's not available outside the function (for example in the return type).