2

In the beginning of chapter 6: Structures of the book by Brian W. Kernighan and Dennis M. Ritchie, there is a paragraph that I can't understand.

The main change made by the ANSI standard is to define structure assignment - structures may be copied and assigned to, passed to functions, and returned by functions. This has been supported by most compilers for many years, but the properties are now precisely defined. Automatic structures and arrays may now also be initialized.

What does it mean that automatic structures and arrays may now also be initialized? I'm pretty sure that automatic, namely local variables should be initialized manually. Would you please help me understand what it means?

Sankalp
  • 2,796
  • 3
  • 30
  • 43

2 Answers2

9

In pre-standard C (meaning 'before the C89 standard', or a long time ago), you could not write:

int function(int i)
{
    int array[4]                 = { 1, 2, 3, 4 };
    struct { int x; int y; } x   = { 1, 2 };
    struct { int x; int y; } a[] = { { 2, 3 }, { 3, 4 } };
    ...code using array, x, a...
    return x.y + a[!!i].x + array[3];
}

Now you are allowed to do all these.

Also, in K&R 1st Edition C (circa 1978), you were not allowed to write:

int another()
{
     struct { int x; int y; } a, b;
     a.x = 1;
     a.y = 0;
     b = a;  /* Not allowed in K&R 1 */
     some_other_func(a, b);    /* Not allowed in K&R 1 */
     some_other_func(&a, &b);  /* Necessary in K&R 1 */
     ...
}

You could also only return pointers to structures (as well as pass pointers to structures). IIRC, some C compilers actually allowed the non-pointer notation, but converted the code behind the scenes to use pointers. However, the structure assignment and structure passing and structure returning limitations were removed from C well before the standard (shortly after K&R 1 was published). But not all compilers supported these features because there wasn't a standard for them to conform to.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Wow. Thank you for your such a nice description. I have a question. The book said that 'Automatic' structures and arrays may now also be initialized. Does that mean global structures and arrays can't be initialized? –  Jul 07 '13 at 16:21
  • 1
    No; it meant that global structures and arrays always could be initialized. – Jonathan Leffler Jul 07 '13 at 16:26
  • Just for the record — declaring the structure types as done in my examples means that the types are not accessible outside the function, so the `some_other_func()` probably can't legitimately be called, etc. This is mostly tangential to the point I'm making, so I'm not about to change the code, but noting it here to satisfy nitpickers (such as me) who might come along later. When I said 'global structures and arrays', I also omitted 'static'; static structures and arrays in functions could always be initialized. C89 introduced 'first element of union' initialization; C99 allows any element. – Jonathan Leffler Jul 07 '13 at 18:53
  • one more important thing to remember about `struct` initialization by direct assignment: non specified values default to zero (`NULL` or 0), *not* to undefined. – Hubert Kario Jul 07 '13 at 19:03
3

It means you can do stuff like this:

typedef struct { int a; float b; } foo;

int main(void) {
    foo f = { 42, 3.14 };  // Initialization

    ...
}
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680