chadathin
chadathin

Reputation: 73

Incompatible pointer types with array of structs

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

Upvotes: 0

Views: 372

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 222312

Two declarations of a structure in different scopes declare different types, per C 2018 6.7.2.3 5: “Two declarations of structure, union, or enumerated types which are in different scopes or use different tags declare distinct types.”

In struct Vehicles *readFile(char file_name[16]) {…, the structure declaration is at file scope.

The declaration inside the function, struct Vehicles {… is at block scope.

Therefore, these two uses of struct Vehicles, even though they use the same tag, refer to different types.

Upvotes: 1

Martin Heralecký
Martin Heralecký

Reputation: 5779

You define the struct inside the function, which means it can only be used inside the scope of the function (its body). Thus, you can't make it a return type.

Move the struct out of the function's body:

struct Vehicles {
    char vehicle[16];
    float engine;
    int price;
    char color[16];
};

struct Vehicles *readFile(char file_name[16]) {
    struct Vehicles *inventory = malloc(sizeof(struct Vehicles)*100);
    // ...
}

Upvotes: 1

Related Questions