【发布时间】:2022-01-01 13:13:38
【问题描述】:
我现在正在做一个学校项目,我需要将两个结构定义为地址,如下面的代码所示:
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
*/
如果我让它喜欢它,它就会编译。但是,一旦我尝试用括号替换分号来定义结构,就会出现编译错误。这是代码和错误:
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];
}
还有错误:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
53 | typedef struct board_t* board{
我需要做的是创建一个板子,里面装满了我可以在函数内部编辑的片段。 谁能帮帮我?
【问题讨论】:
-
完全避免 typedef。使用 typedef 来掩盖类型是指针的事实是持续维护麻烦的秘诀。在某些情况下 typedef 很有用,但应谨慎使用。
-
@Exampleperson 您可以在不使用 typedef 的情况下制作不透明的结构。
-
IMO,typedef 唯一合理的地方是函数指针。
-
typedef 非常适合结构,因此您不需要将“struct”作为类型名的一部分,stackoverflow.com/questions/252780/… 我可能不会使用它们来隐藏新类型名的指针性质尽管。但归根结底,编译器设置规则,其他一切都只是一个指导方针,我们需要根据项目的需要使用我们的判断。
-
我本来也想这样,但是老师让我们那样做,太糟糕了:(
标签: c struct definition