Reputation: 1212
I have the following code with an intention to initialize member b. This should happen for all the MAX_SIZE structs.
enum { MAX_SIZE = 10 };
struct some
{
int a, b;
}
many[MAX_SIZE] = { {.b = 5} };
int main()
{
int i;
for (i = 0; i < MAX_SIZE; i++)
{
printf("%d, %d\n", many[i].a, many[i].b);
}
}
I need the output to look like:
0, 5
0, 5
0, 5
... (10 times)
But, the actual output is:
0, 5
0, 0
0, 0
... (10 times)
How to get the required output without requiring an explicit for loop for assigning the values? I know in C++, this is accomplished by providing a constructor for the struct initializing b only.
Upvotes: 1
Views: 895
Reputation: 190
It's not C Standard, but with this gcc extension you can do this :
struct some many[10] = { [0 ... 9].b = 5 };
It works with clang >= 5
too.
Upvotes: 2