Search code examples
carraysstatic

Variably modified array at file scope in C


I have some code like this:

static int a = 6;
static int b = 3;

static int Hello[a][b] =
{
    { 1,2,3},
    { 1,2,3},
    { 1,2,3},
    { 1,2,3},
    { 1,2,3},
    { 1,2,3}
};

But when I compile it, it says error:

variably modified 'Hello' at file scope

How could this happen? And how could I fix it?


Solution

  • You can not have a static array whose size is given as a variable.

    That's why constants should be #defined:

    #define a 6
    

    This way, the preprocessor will replace a with 6, making it a valid declaration.