How were arrays initialized in K&R C?

Viewed 125

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'.

1 Answers

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.
Related