c - Struct Initialization Error -
i know how initialize structs (generally), i'm having troubles struct within struct stuff
typedef struct location{ uint8_t x; uint8_t y; } loc; typedef struct person{ loc location; } person; global variables:
static person hero; initialization functions:
void initializehero() { person hero = {0,0, {0,0}}; // compiles hero.location = {0,0}; // not compile hero = {0,0,{0,0}}; // not compile }
your 'this compiles' line correct; that's initialization. other 2 lines don't compile, because they're not initializations, they're assignments. if you're using new enough version of c, use compound literal assignments:
hero.location = (loc){0,0}; hero = (person){0,0,{0,0}}; note - person hero declaration in initializehero shadows global variable; don't want that.
btw, missing fields in person? none of should compile you've shown.
Comments
Post a Comment