До сих пор всегда ( в IAR и Builder ) пользовался такой фишкой:
Цитата
В стандарте С99 (6.7.8) есть интересная вещь - так называемые designators (обозначения?). Их использование позволяет инициализировать элементы структур при помощи списка инициализации не обращая внимания на порядок следования элементов. Например
struct StTest {
int i;
char c;
double d;
};
int main(int argc, char *argv[])
{
struct StTest st1={.d=0.1, .i=1, .c='a'},
st2={.c='b',.d=0.2,.i=2};
printf("st1.i= %d st1.c= %c st1.d= %f\n",st1.i,st1.c,st1.d);
printf("st2.i= %d st2.c= %c st2.d= %f\n",st2.i,st2.c,st2.d);
return EXIT_SUCCESS;
}
А вот собственно и результат работы такой програмки
commander@a64:$ ./teststructinit
st1.i= 1 st1.c= a st1.d= 0.100000
st2.i= 2 st2.c= b st2.d= 0.200000
Такая вещь бывает очень полезна, так как нет необходимости помнить порядок следования элементов структуры.
© Yuriy Volkov
struct StTest {
int i;
char c;
double d;
};
int main(int argc, char *argv[])
{
struct StTest st1={.d=0.1, .i=1, .c='a'},
st2={.c='b',.d=0.2,.i=2};
printf("st1.i= %d st1.c= %c st1.d= %f\n",st1.i,st1.c,st1.d);
printf("st2.i= %d st2.c= %c st2.d= %f\n",st2.i,st2.c,st2.d);
return EXIT_SUCCESS;
}
А вот собственно и результат работы такой програмки
commander@a64:$ ./teststructinit
st1.i= 1 st1.c= a st1.d= 0.100000
st2.i= 2 st2.c= b st2.d= 0.200000
Такая вещь бывает очень полезна, так как нет необходимости помнить порядок следования элементов структуры.
© Yuriy Volkov
А KEIL на такую инициализацию ругается "main.cpp(542): error: #29: expected an expression". В IAR тот же код компилится без проблем.
И еще примеры из стандарта
Цитата
1702 EXAMPLE 10 Structure members can be initialized to nonzero values without depending on their order:
div_t answer = { .quot = 2, .rem = -1 };
1703 EXAMPLE 11 Designators can be used to provide explicit initialization when unadorned initializer lists might be misunderstood:
struct { int a[3], b; } w[] =
{ [0].a = {1}, [1].a[0] = 2 };
1704 EXAMPLE 12 Space can be “allocated” from both ends of an array by using a single designator:
int a[MAX] = {
1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0
};
In the above, if MAX is greater than ten, there will be some zero-valued elements in the middle; if it is less than ten, some of the values provided by the first five initializers will be overridden by the second five.
1705 EXAMPLE 13 Any member of a union can be initialized:
union { /* ... */ } u = { .any_member = 42 };
div_t answer = { .quot = 2, .rem = -1 };
1703 EXAMPLE 11 Designators can be used to provide explicit initialization when unadorned initializer lists might be misunderstood:
struct { int a[3], b; } w[] =
{ [0].a = {1}, [1].a[0] = 2 };
1704 EXAMPLE 12 Space can be “allocated” from both ends of an array by using a single designator:
int a[MAX] = {
1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0
};
In the above, if MAX is greater than ten, there will be some zero-valued elements in the middle; if it is less than ten, some of the values provided by the first five initializers will be overridden by the second five.
1705 EXAMPLE 13 Any member of a union can be initialized:
union { /* ... */ } u = { .any_member = 42 };