6

I know how to initialize structs (generally), but I'm having troubles with this struct within a 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}}; // this compiles
    hero.location = {0,0}; // but this does not compile
    hero = {0,0,{0,0}}; // this also does not compile
}
alk
  • 69,737
  • 10
  • 105
  • 255
Justin
  • 61
  • 2
  • 1
    Huh? Your first initialization does *not* compile. You have 4 scalars inside `{ }` and only 2 scalar members in the struct. This is an error in C. – AnT stands with Russia Apr 25 '15 at 00:25
  • More or less a duplicate to: http://stackoverflow.com/q/24138140/694576 – alk Apr 25 '15 at 07:09
  • possible duplicate of [How to initialize a struct in ANSI C](http://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-ansi-c) – alk Apr 25 '15 at 07:12
  • Ah yes my apologies. I'm new to Stack Overflow and when I was editing my code to make the syntax fit with how the system wanted it I must have accidentally deleted part of the struct. The struct Person initially had 4 scalar members {uint8_t x; uint8_t y; loc location} – Justin Apr 26 '15 at 06:52

2 Answers2

3

Your 'this compiles' line is correct; that's an initialization. The other two lines don't compile, because they're not initializations, they're assignments. If you're using a new enough version of C, you could use a compound literal to do the assignments:

hero.location = (loc){0,0};
hero = (person){0,0,{0,0}};

Note - your person hero declaration in InitializeHero shadows the global variable; you probably don't want that.

BTW, are you missing some fields in your person? None of that should compile with what you've shown.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

The line you indicate that compiles produces a warning. Let's break apart your InitializeHero function.

person hero = {0,0, {0,0}};

Here you are instantiating your new person struct named hero. You use the brace initialization method to set the members of the struct. In this case, the only member of person is a loc. A loc itself only has two uint8_ts. Using the brace initialization method here, you would simply use {0, 0}.

Combining these two, you could write a statement like:

person hero = {{0, 0}};

Note that you can only use brace initialization when initializing. Your other two statements are assignments. The struct has already been initialized at this point, which is why those two statements don't compile.

One other note, your global variable static person hero has been shadowed by the local variable hero in InitializeHero. This means that you are creating a separate person struct in your InitializeHero. However, this static variable is initialized where it is declared in this case, so your statement must read

static person hero = {{0, 0}};

...leaving InitializeHero unused.

greg
  • 4,843
  • 32
  • 47
  • 1
    The function would still have a purpose if it set the global variable (and therefore did not declare the local variable). You sometimes need to reinitialize a global variable and a function like that might be appropriate. – Jonathan Leffler Apr 25 '15 at 04:37