1

So this section of code generates a huge amount of errors but it works when I have InputM[3][3] = blah

Why would this be. For reference, code:

int n = 3;
printf("%ld\n", n);
double InputM[n][n] = { { 2, 0, 1 }, { 3, 1, 2 }, { 5, 2, 5} };

Generates:

prog3.c: In function 'main':
prog3.c:47: error: variable-sized object may not be initialized
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM[0]')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM[0]')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM[0]')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM[0]')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM[0]')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM[0]')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM[0]')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM[0]')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM[0]')
prog3.c:47: warning: excess elements in array initializer
prog3.c:47: warning: (near initialization for 'InputM')
alk
  • 69,737
  • 10
  • 105
  • 255
SaltySeaDog
  • 45
  • 2
  • 6
  • possible duplicate of [C compile error: "Variable-sized object may not be initialized"](http://stackoverflow.com/questions/3082914/c-compile-error-variable-sized-object-may-not-be-initialized) – Nikos C. Nov 07 '13 at 11:35

2 Answers2

3

Compile-time, you compiler does not know how many elements are in your matrix. In C, you can dynamically allocate memory using malloc.

You could use a define to create a constant value:

#define N 3

int main()
{
    double InputM[N][N] = { { 2, 0, 1 }, { 3, 1, 2 }, { 5, 2, 5} };
}

Or malloc:

int main()
{
    int n = 3;
    int idx;
    int row;
    int col;

    double **inputM;
    inputM = malloc(n * sizeof(double *));
    for (idx = 0; idx != n; ++idx)
    {
        inputM[idx] = malloc(n * sizeof(double));
    }

    // initialise all entries on 0
    for (row = 0; row != n; ++row)
    {
        for (row = 0; row != n; ++row)
        {
            inputM[row][col] = 0;
        }
    }

    // add some entries
    inputM[0][0] = 2;
    inputM[1][1] = 1;
    inputM[2][0] = 5;
}
Arjen
  • 636
  • 1
  • 7
  • 13
  • Thank you. Does that make a matrix which only has one dimention? – SaltySeaDog Nov 07 '13 at 11:45
  • Thers is not need to do ` idx != n;`, doing ` idx < n;` is more save. Also in C is not necessary nor ecommended to cast the result of `malloc/calloc/realloc`: http://stackoverflow.com/a/605858/694576 – alk Nov 07 '13 at 11:46
  • @alk About the casting: agree, it was a leftover from some C++ error.About the `!=' I do not agree. If you are not computing anything in parallel, this is fine and probably faster. I also think it is more readable. – Arjen Nov 07 '13 at 11:50
  • @SaltySeaDog No, it allocates a 2D array, I will add an example on how you can use it. – Arjen Nov 07 '13 at 11:51
  • This answer is misleading. As the other answers provide, C knows very well how to deal with variables as bounds to the array. The only thing that it doesn't know is how to initialize such a beast. This is clearly indicated in the error message that the compiler gave. So you'd have to initialize it by assignment of the individual entries in a `for` loop, that's all. – Jens Gustedt Nov 07 '13 at 12:26
  • It will work, but it is not a nice way to do. Better use malloc yourself and you will understand better what is going on. – Arjen Nov 07 '13 at 12:34
1

In C99, variable-sized array can't be initialized, why ?

Because at the compile time, the compiler doesn't know the exact size of array, so you cannot initialize it.

n will be evaluated at runtime, then your array will be allocated on the stack-frame.

linkdd
  • 1,015
  • 1
  • 10
  • 24