【发布时间】:2015-04-24 23:35:58
【问题描述】:
我知道如何初始化结构(通常),但我遇到了麻烦 结构内的结构
typedef struct Location{
uint8_t x;
uint8_t y;
} loc;
typedef struct Person{
loc location;
} person;
全局变量:
static person hero;
初始化函数:
void InitializeHero() {
person hero = {0,0, {0,0}}; // this compiles
hero.location = {0,0}; // but this does not compile
hero = {0,0,{0,0}}; // this also does not compile
}
【问题讨论】:
-
嗯?您的第一次初始化不会 编译。
{ }中有 4 个标量,结构中只有 2 个标量成员。这是 C 语言中的一个错误。 -
啊,是的,我很抱歉。我是 Stack Overflow 的新手,当我编辑代码以使语法符合系统的要求时,我一定是不小心删除了结构的一部分。结构 Person 最初有 4 个标量成员 {uint8_t x; uint8_t y;本地位置}
标签: c struct initialization