3

In K&R C(2nd) 127p,

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's the meaning of 'Automatic structures and arrays may now also be initialized.'? I read this.
And It's written at 261p(the part of appendix C. summary of changes)

Automatic structures, unions, and arrays may be initialized, albeit in a restricted way.

I got following code is not allowed in K&R C(1st).

int main()
{
    int a[3] = { 1, 2, 3 };
    struct { int a; char b; } x = { 1, 2 };
}

Could you explain then how were arrays and structures initialized in that time?
It's a bit confusing where to focus, 'automatic' to 'global' or 'now be initialized' to 'couldn't be initialized'.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
op ol
  • 705
  • 4
  • 11
  • 1
    `structure assignment-structures` refers to the `{ 1, 2, 3 }` and `{ 1, 2 }`. `Automatic structures, unions, and arrays may be initialized` is hinting the fact that these assignment structures can be used to [initialize](https://en.cppreference.com/w/c/language/initialization) the arrays (the word automatic means [automatic storage](https://en.cppreference.com/w/c/language/storage_duration)) - which is the default storage specifier for block scoped variables. – Chase Dec 29 '20 at 13:48
  • 1
    In, K&R C - you'd declare the array with `int a[3]`, then assign the values by looping through the indices and assigning the value to each of them – Chase Dec 29 '20 at 13:48

1 Answers1

2

As far as I understand, that point of time initialization at the time of definition were not allowed for structure, union and array type variables which automatic storage. They would have to be assigned values, using separate assignment statements. Example:

  • For array, it'll be looping over elements and assigning one by one.
  • For structures, accessing members and assigning them.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261