A problem with designated initializers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ark

    #1

    A problem with designated initializers

    As a const data tables freak, I find it distressing that
    const int foo[10] = {
    [5] = 8,
    [5] = 9,
    };
    compiles by IAR (EWARM 4.40a) without any warnings. (Nor does PC-Lint
    8.00u see anything wrong.)
    The actual content at an index happens to be the last initializer for it.
    Is this indeed legal? If so, what's the rationale behind it?
    Thanks,
    - Ark
  • Harald van Dijk

    #2
    Re: A problem with designated initializers

    Ark wrote:
    As a const data tables freak, I find it distressing that
    const int foo[10] = {
    [5] = 8,
    [5] = 9,
    };
    compiles by IAR (EWARM 4.40a) without any warnings. (Nor does PC-Lint
    8.00u see anything wrong.)
    The actual content at an index happens to be the last initializer for it.
    Is this indeed legal?
    Yes, it is.
    If so, what's the rationale behind it?
    I don't know if this is what was intended, but one example where it can
    help is when you want the whole array to be initialised to a specific
    value, /except/ for one element.

    int arr[10] = {
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    [5] = 0,
    };

    You could rewrite this as 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, of course, but
    that doesn't work when 5 is not as fixed a constant as it is here.

    Comment

    Working...