3

I can't understand how the following structure elements are being initialized.

MSG m = {4, 1, 0};.

MSG has two elements: one is of type int and other one is an array of type short that's already been initialized. Which elements of m are assigned the values 4, 1 and 0? Here is the listing of the code taken from the book TCP IP in C 2nd Ed. (Page No. xi, Preface);

typedef struct {
    int a;
    short s[2];
} MSG;
MSG *mp, m = {4, 1, 0};
char *fp, *tp;
mp = (MSG *) malloc(sizeof(MSG));
for (fp = (char *)m.s, tp = (char *)mp->s; tp < (char *)(mp+1);)
  *tp++ = *fp++;

Although I've read over C a reference manual but I couldn't find the solution. I might have overlooked it but I can't proceed until I clear my understanding how the initialization is done where an element of m is a three element array of type short int itself.

  • turn on the compiler warnings. Then the compiler will tell you about the missing braces '{' '}' around the last two elements. – user3629249 Sep 24 '16 at 21:19
  • Thanks. Yes, I did as gcc -Wall -W -g3 <file.c> and it spewed the warning; warning: missing braces around initializer [-Wmissing-braces] MSG *mp, m = {4, 5, 9}; ^ I couldn't understand what it meant. – Bishnu Sep 25 '16 at 13:59
3

Do it like this:

MSG m = {
.a = 4 
.s = {1, 0}
};

It's clearer, it works, and you won't have to fry brain cells figuring out what it does every time you look at it.

  • Thank you very much. I got it. I also got that I need to know a lot before I soil my hands with tcp/ip programming. :) – Bishnu Sep 23 '16 at 14:54
3

Correct way to initialize the structure is shown in Robert's answer, but I guess the question is "how this initialization works?"

In fact, the C reference does describe structure initialization and it allows you to not use extra curly braces as long as the number of elements in initializer is less or equal to number of elements in aggregate. Following snippets are equivalent:

MSG m = {4, 1, 0};

and

MSG m = {4, {1, 0}};

But this one is incorrect, since a element is not an aggregate:

MSG m = {{4}, {1, 0}};

I only have Russian copy of KR, so here's a rough translation: Aggregate initializer starts with curly and all the items inside those braces are considered part of that aggregate. If it doesn't start with curly, it takes as much elements as it can hold (or less if there is not enough elements).

Surprisingly, the following two snippets are also correct:

MSG m = {4, 1};

and

MSG m = {4, {1}};
  • Thanks. The statement MSG m = {{4}, {1, 0}}; seems interesting and in this case I'm afraid I haven't got a clear concept what an aggregate type/kind is. – Bishnu Sep 23 '16 at 15:04
  • 1
    The aggregate is defined in the same part in the specs - basically it is anything that is not a primitive type - structure or array. – aragaer Sep 24 '16 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.