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
}
4
  • 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. Commented Apr 25, 2015 at 0:25
  • More or less a duplicate to: stackoverflow.com/q/24138140/694576 Commented Apr 25, 2015 at 7:09
  • possible duplicate of How to initialize a struct in ANSI C Commented Apr 25, 2015 at 7: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} Commented Apr 26, 2015 at 6:52

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes my bad. Sorry about the missing fields (are these called scalars?)
Also, what is the purpose of compound literals? Are they for clarity? Because they seem kind of redundant
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.