0

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 ?

7
  • Works on my machine Commented Apr 16, 2016 at 21:27
  • It's required behaviour for C compilers; 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, use enum { LABEL_NUMBER = 4 };. That will appear in your symbol table but can be used in array dimensions. Commented Apr 16, 2016 at 21:29
  • 2
    It's curious to use string concatenation on the two shortest strings in the initializers. ("By ... " "& ...", is a single string because there's no comma after the second double quote.) Commented Apr 16, 2016 at 21:31
  • @JonathanLeffler Ok tanks i've understood :) Commented Apr 16, 2016 at 21:39
  • 2
    @MooingDuck this is a C question; you're using a C++ compiler Commented Apr 17, 2016 at 5:56

2 Answers 2

1

Variable length arrays can't be initialised using initializers.

C11 - §6.7.9/3:

The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm sort of shocked that it compiles: coliru.stacked-crooked.com/a/bb6ad14a4c89e222
@MooingDuck; In c++, const qualified objects are treated as real constant literals (with some exceptions). You are compiling using g++. It's a C++ compiler.
But i don't understand why LABEL_TEXT[] is a VLA, char *LABEL_TEXT[const ...] == char *(LABEL_TEXT[const ...]) no ?
@Maxime; In C, const qualified objects are not constant literals.
0

You can define LABEL_NUMBER as macro:

#define LABEL_NUMBER 4

That way the LABEL_NUMBER

const char *LABEL_TEXT[LABEL_NUMBER]

will be replaced at pre-processing phase by defined macro value which will produce:

const char *LABEL_TEXT[4]

for a compiler to compile.

1 Comment

Thank you for your answer but it isn't really what i was asking :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.