A problem with designated initializers

A

Ark

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
 
G

Guest

Ark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top