Search code examples
cstructdeclarationreturn-typefunction-definition

Can you return a struct that is declared inside that function?


I want to return the struct that is declared, and values set inside the initializePlayer() function so I can call it on the main function.
This is a task and we are not allowed to declare global variables (only inside of functions) so I kind of think that I need to declare the struct inside functions—unless it's not a variable(?) I don't really know :(

Here is my code and I can't get it to work

struct Player initializePlayer(int playerNumber)
{
    struct Player
    {
        int position[1][2];
        double money;
        int propertiesOwned[10];
    } player[4];

    // Set player's position to starting tile
    player[playerNumber].position[0][0] = 5;
    player[playerNumber].position[0][1] = 0;
    // Set player's default money
    player[playerNumber].money = 300000;
    // Set player's properties to none
    for (int i; i <= 10; i++)
    {
        player[playerNumber].propertiesOwned[i] = 0;
    }
    // Player initialized

    return player[playerNumber];
}

int main()
{
    // Initializes player 1 and 2
    struct Player p1 = initializePlayer(1);
    struct Player p2 = initializePlayer(2);
    return 0;
}

If I declare the struct to global however, it works just fine.

I want the function to return a struct, on which that struct is declared inside that function


Solution

  • This is a task and we are not allowed to declare global variables (only inside of functions)

    Type specifiers are not variables.

    You need to declare a structure before its using.

    Also in a function definition the return type shall be a complete object type. From the C Standard (6.9.1 Function definitions)

    3 The return type of a function shall be void or a complete object type other than array type.

    So this function definition

    struct Player initializePlayer(int playerNumber)
    {
        struct Player
        {
            int position[1][2];
            double money;
            int propertiesOwned[10];
        } player[4];
        //...
    

    is wrong. The return type of the function is not a complete object type.

    You need to define the structure before the function definition like

        struct Player
        {
            int position[1][2];
            double money;
            int propertiesOwned[10];
        };
    

    Also it is better to pass to the function an object of the structure type by reference through a pointer to it.

    In this case the function declaration will look like

    void initializePlayer( struct Player *player )
    {
        // Set player's position to starting tile
        player->position[0][0] = 5;
        player->position[0][1] = 0;
        // and so on
    

    The function return type will be void.

    And the array of objects of the structure type will be declared in main

    int main( void )
    {
        struct Player player[4] = { 0 };
    
        // Initializes player 1 and 2
        initializePlayer( &player[1] );
        initializePlayer( &player[2] );
    
        return 0;
    }