Search code examples
cstructinitializationcompound-literals

Any way to initialize a dynamically allocated structure?


C11 gives us a neat struct initialization syntax:

struct some_struct {int some; char value;};
struct some_struct s = {.some = 5, .value = 'a'};
printf("some = %d, value = %c\n", s.some, s.value);

http://ideone.com/zZxTc4

However, it doesn’t seem to work when the struct has to be dynamically allocated:

struct some_struct {int some; char value;};
struct some_struct *s = malloc(sizeof(struct some_struct));
*s = {.some = 5, .value = 'a'};
printf("some = %d, value = %c\n", s->some, s->value);

This code yields:

error: expected expression before ‘{’ token
  *s = {.some = 5, .value = 'a'};
       ^

http://ideone.com/nBiorw

struct some_struct {int some; char value;};
struct some_struct *s = malloc(sizeof(struct some_struct));
*s = struct some_struct {.some = 5, .value = 'a'};
printf("some = %d, value = %c\n", s->some, s->value);

And this code yields:

error: expected expression before ‘struct’
  *s = struct some_struct {.some = 5, .value = 'a'};
       ^~~~~~

http://ideone.com/SpBX1E

Is there any way in C to initialize a dynamically allocated struct that nicely or do I rather have to write something like that:

struct some_struct {int some; char value;};
struct some_struct *s = malloc(sizeof(struct some_struct));
s->some = 5;
s->value = 'a';
printf("some = %d, value = %c\n", s->some, s->value);

http://ideone.com/1XAiQS

Which is slightly more inconvenient because (a) it means each member of the struct has to occupy its own line since people tend to frown when they see a ; sign not being the last sign of its line in a source code, and (b) I’m not able to rely on unspecified values being initialized to 0 as happens in normal struct initialization.


Solution

  • In your code

     *s = {.some = 5, .value = 'a'};
    

    is not an initialization, it's an assignment.

    Brace enclosed initializer can only be used during initialization.

    You can use a compound literal, though. Something like

    *s = (struct some_struct){.some = 5, .value = 'a'};
    

    will do the job.