I was coding a mini GTK+2.0 game when i had a problem. When i write this :
const unsigned LABEL_NUMBER = 4;
const char *LABEL_TEXT[4] = {
"Five or More",
"By ... "
"& ...",
"April 2016",
"~~ Thanks for playing ~~"
};
There is no problem. But when i write this :
const unsigned LABEL_NUMBER = 4;
const char *LABEL_TEXT[LABEL_NUMBER] = {
"Five or More",
"By ... "
"& ...",
"April 2016",
"~~ Thanks for playing ~~"
};
gcc answers :
source/gui.c: In function ‘create_about_window’:
source/gui.c:202:4: error: variable-sized object may not be initialized
const char *LABEL_TEXT[LABEL_NUMBER] = {
^
source/gui.c:203:34: error: excess elements in array initializer [-Werror]
"Five or More",
^
source/gui.c:203:34: note: (near initialization for ‘LABEL_TEXT’)
source/gui.c:204:34: error: excess elements in array initializer [-Werror]
"By ... & ..."
^
source/gui.c:204:34: note: (near initialization for ‘LABEL_TEXT’)
source/gui.c:206:34: error: excess elements in array initializer [-Werror]
"April 2016",
^
source/gui.c:206:34: note: (near initialization for ‘LABEL_TEXT’)
source/gui.c:207:34: error: excess elements in array initializer [-Werror]
"~~ Thanks for playing ~~"
^
source/gui.c:207:34: note: (near initialization for ‘LABEL_TEXT’)
So i just want to know why gcc displays this errors while i use a constant unsigned integer to set the array size ?
const unsigned LABEL_NUMBER = 4;is a variable — albeit one that doesn't change value. Arrays come in two flavours; those with a size fixed by a compile time integer constant (which can be initialized), and those with a variable size (which cannot be initialized). Because, in the terms of the C compiler (C standard), the latter is a variable, you have a variably-modified array and can't use initializers. In case of doubt, useenum { LABEL_NUMBER = 4 };. That will appear in your symbol table but can be used in array dimensions."By ... " "& ...",is a single string because there's no comma after the second double quote.)