-1

I have a structure with multiple members inside of it that I can't seem to be able to define after declaring it in a separate statement. For simplicity sake, suppose it looks like the following

typedef struct 
{
int number;
int anotherNumber;
char character;
char anotherCharacter;
} MY_STRUCTURE_t;

It seems like I should be able to declare the structure and define it later on, as I would do with a variable of a simpler type. For example,

MY_STRUCTURE_t my_structure;

my_structure = {
.number = 1,
.anotherNumber = 42,
.character = '\0',
.anotherCharacter = 'a'
};

The code above generates an error: "Invalid redeclaration of "my_structure"". Fair enough, the compiler knows better. On the other hand, the following code works fine.

MY_STRUCTURE_t my_structure = {
.number = 1,
.anotherNumber = 42,
.character = '\0',
.anotherCharacter = 'a'
};

What is the problem with the snippet in the middle? Is there a fundamental flaw that I am missing?

fandor
  • 213
  • 3
  • 11
  • Look up definition and declaration. See this [question](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration) – t0mm13b May 11 '16 at 22:58
  • The RHS of that assignment is an initializer. By definition an initializer can only be used to set a variable at the point it is defined. The initializer syntax is not valid for subsequent assignment operations. – kaylum May 11 '16 at 23:10
  • Unfortunately that code can only be used for declarations. It is not built for assigning new values to a struct members. You will have to call individual members instead. – Striker May 11 '16 at 23:14
  • 1) Don't use all-uppercase-names (or ones like the ones you use); they should only be used for macros and _enum-constants_ 2) The suffix `_t` is reserved by some standards like POSIX for system libraries. – too honest for this site May 11 '16 at 23:28
  • @Striker: A declaration with an initialiser is a _definition_. – too honest for this site May 11 '16 at 23:29
  • `MY_STRUCTURE_t my_structure; my_structure.number = 1; my_structure.anotherNumber = 42; my_structure.character = '\0'; my_structure.anotherCharacter = 'a';` – user3078414 May 11 '16 at 23:31
  • Thank you for taking time to read my question. Can I kindly ask for the reason the question was downvoted? I would like to improve it, if there's still a chance, or not make the same mistake later. Cheers! – fandor May 11 '16 at 23:37

1 Answers1

2

This line:

MY_STRUCTURE_t my_structure;

is a definition. You say "declare it now and define it later" but actually what you are describing is "define it now, and assign a new value later". This is the same for primitive types.

In C, assignment (not initialization) cannot be done directly from a brace-enclosed list. But you can (at block scope of course) assign from a compound literal:

my_structure = (const MY_STRUCTURE_t){
    .number = 1,
    .anotherNumber = 42,
    .character = '\0',
    .anotherCharacter = 'a'
};
M.M
  • 130,300
  • 18
  • 171
  • 314
  • Thank you for your answer! Are compound literals something that you would frequently see/use in practice? Especially in the context of embedded systems? – fandor May 13 '16 at 17:13
  • sometimes embedded system designers don't realize that it's not the 1980s any more – M.M May 14 '16 at 00:11