所以我对C(以及一般的编程)很陌生,我想使用struct作为枚举的值
typedef struct {
int x;
int y;
} point;
// here's what I'd like to do
enum directions {
UP = point {0, 1},
DOWN = point {0, -1},
LEFT = point {-1, 0},
RIGHT = point {1, 0}
};
Run Code Online (Sandbox Code Playgroud)
因此,之后我可以使用枚举来执行坐标转换
如果您了解我想要实现的目标,请解释为什么这不起作用和/或正确的方法是什么?
enum仅用于将"魔术数字"翻译成文本和有意义的东西.它们只能用于整数.
你的例子比那更复杂.看起来你真正想要的是一个结构,包含4个不同的point成员.可能const合格.例:
typedef struct {
int x;
int y;
} point;
typedef struct {
point UP;
point DOWN;
point LEFT;
point RIGHT;
} directions;
...
{
const directions dir =
{
.UP = (point) {0, 1},
.DOWN = (point) {0, -1},
.LEFT = (point) {-1, 0},
.RIGHT = (point) {1, 0}
};
...
}
Run Code Online (Sandbox Code Playgroud)