0

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

3
  • 3
    Think about scope! You define the structure Vehicles inside the function, which means it's not available outside the function (for example in the return type). Commented Feb 1, 2020 at 15:57
  • Ah! Okay. I should have defined the struct outside the function. THEN created a specific instantiation inside the function? Commented Feb 1, 2020 at 16:03
  • where have you found such a strange information Commented Feb 1, 2020 at 16:05

2 Answers 2

1

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);
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's the one! Thank you very much for your help! I have to remember; defining a struct is kind of like defining a new type of variable. Therefore, everything that would apply to scope with any other type of variable would also apply to structs.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.