Нашёл тут недавно в стандарте C вот такое:
The declaration
int y[4][3] = {
{ 1, 3, 5 },
{ 2, 4, 6 },
{ 3, 5, 7 },
};
is a definition with a fully bracketed initialization: 1, 3, and 5 initialize the first row of y (the array object
y[0]), namely y[0][0], y[0][1], and y[0][2]. Likewise the next two lines initialize y[1] and
y[2]. The initializer ends early, so y[3] is initialized with zeros. Precisely the same effect could have
been achieved by
int y[4][3] = {
1, 3, 5, 2, 4, 6, 3, 5, 7
};
The initializer for y[0] does not begin with a left brace, so three items from the list are used. Likewise the
next three are taken successively for y[1] and y[2].
По-моему, выглядит ужасно, да и ошибки сложно заметить будет, зато скобки лишние писать не надо.
Меня за такой code-style никто не побьёт?
int a[4][3] = {
1, 3, 5, 2, 4, 6, 3, 5, 7
};
В C++ такое тоже по стандарту, да?
Здравствуйте, Аноним, Вы писали:
А>Меня за такой code-style никто не побьёт?
А>А>int a[4][3] = {
А>1, 3, 5, 2, 4, 6, 3, 5, 7
А>};
А>
Замени тройку на четверку (a[4][4]) в размере массива и сравни результаты инициализации со скобками и без.
А>В C++ такое тоже по стандарту, да?
Да.
Здравствуйте, Аноним, Вы писали:
А>По-моему, выглядит ужасно, да и ошибки сложно заметить будет, зато скобки лишние писать не надо.
сравни:
struct Data
{
int a;
char * ptr;
std::string str;
double p;
std::string str2;
};
struct Data [3] =
{
{ 0, NULL, "", 0, "" },
{ 5, "hello ptr", "hello str", 5.7, "hello str2" },
{ 18, "bugi", "vugi", 0.3388, "end message" }
};
struct Data [3] = {
0, NULL, "", 0, "", 5, "hello ptr", "hello str", 5.7, "hello str2", 18, "bugi", "vugi", 0.3388, "end message"
};