I'm working on a school project right now and i need to define two structs as addresses, like the code below shows :
typedef struct board_t* board;
/**
* @brief Pointer to the structure that holds the game.
*/
typedef struct piece_t* piece;
/**
* @brief Pointer to the structure that holds a piece
*/
and if i let it like it, it compiles. However, as soon as I try replacing the semicolon by a bracket to define the struct, I get a compilation error. Here's the code and the error :
typedef struct piece_t* piece{
/**
* @brief Pointer to the structure that holds a piece
*/
enum shape p_shape;
enum size p_size;
enum color p_color;
enum top p_top;
enum players author;
};
typedef struct board_t* board{
/**
* @brief Pointer to the structure that holds the game.
*/
piece array[4][4];
}
And the error :
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
53 | typedef struct board_t* board{
What i need to do is create a board filled with pieces that i can edit inside functions. Can anyone help me?
CodePudding user response:
I think typedef names need to be at the end
typedef struct piece_struct {
/**
* @brief Pointer to the structure that holds a piece
*/
enum shape p_shape;
enum size p_size;
enum color p_color;
enum top p_top;
enum players author;
}
piece;
typedef struct board_struct {
/**
* @brief Pointer to the structure that holds the game.
*/
piece array[4][4];
}
board;
and if you want a typedef name for the pointer, you would need to create those separately.
typedef piece* piece_ptr;
typedef board* board_ptr;
Perhaps the code is clearer if the structure definitions are separated from the typedefs:
struct piece_struct {
/**
* @brief structure that holds a piece
*/
enum shape p_shape;
enum size p_size;
enum color p_color;
enum top p_top;
enum players author;
};
typedef piece_str* piece; // piece is a new name for a pointer
// to a piece_str
struct board_struct {
/**
* @brief structure that holds the game.
*/
piece array[4][4];
};
typedef struct board_struct* board; // board is a new name for a pointer
// to a board_str
Personally I tend not to make typedefs for pointers as I find it difficult to remember whether it is a pointer or the struct itself, so I make a typedef for the struct and use * when declaring a pointer.