'expected ';' at end of declaration list when I have one?

so i've declared a struct which is shown below

struct location {
    char occupier;
    int points;
    int x;
    int y;
    int current_x = PLAYER_STARTING_COL;
    int current_y = PLAYER_STARTING_ROW;
    
};

I have made a struct variable called starting however, when I compile it I get an error saying it expects a semicolon at the end of my declaration list? Is there any way I can fix this? I'm a beginner to C and just need a bit of help

struct location starting;


Solution 1:[1]

In C opposite to C++ you may not initialize data members of a structure in its definition.

So you have to write

struct location {
    char occupier;
    int points;
    int x;
    int y;
    int current_x;
    int current_y;
    
};

and to initialize data members when an object of the structure type is defined as for example

struct location starting = 
{ 
    .current_x = PLAYER_STARTING_COL, .current_y = PLAYER_STARTING_ROW 
};

If the above declaration is a file scope declaration then PLAYER_STARTING_COL and PLAYER_STARTING_ROW must be constant expressions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1