Assignment of array elements in array initialization

Viewed 183

Consider the following simple program:

#include <stdio.h>

int main(void)
{
    int a[5] = { a[2] = 1 };
    printf("%d %d %d %d %d\n", a[0], a[1],a[2], a[3], a[4]);
}

With GCC 7.3.0 this outputs

1 0 1 0 0

Considering that a[1] is zero, it seems that the initialization is similar to

int a[5] = { 1 };
a[2] = 1;

The question is: While initializers could be any generic expression, in which order is the initialization and assignments made?

Is this even valid and well-defined? Could it be implementation-defined, undefined or maybe unspecified?


This question is related to the question Confusion about Array initialization in C.

1 Answers

While I cannot state, that I am an ISO-expert, here is what I found out with the help of godbolt.

First I built the sample with the help of -fsanitize=undefined, which gives a good indication of undefined behavoir. Neither GCC nor clang were complaining.

Next I looked at the various stages gcc performs, in this case the gimple stage

foo ()
{
  int a[5];

  try
    {
      # DEBUG BEGIN_STMT
      a = {};
      a[2] = 1;
      _1 = a[2];
      a[0] = _1;
      # DEBUG BEGIN_STMT
      _2 = a[4];
      _3 = a[3];
      _4 = a[2];
      _5 = a[1];
      _6 = a[0];
      printf ("%d %d %d %d %d\n", _6, _5, _4, _3, _2);
    }
  finally
    {
      a = {CLOBBER};
    }
}

Here you can see, that first the array a is defined, then 1 is assigned to a[2], afterwards that result (1, because it is an assignment) is assigned to the first element of a. The other in indices are left to 0 and therefore the pattern 1 0 1 0 0 is printed out.

Related