0

I have a maybe no-so-smart-question, I am working with C and there is a problem that is causing me some kind of doubts. I have this code:

int main(int argc, char const *argv[]){
    int r=3, c=4;
    int matrix[r][c] = {{1,2,3,4},{1,2,3,4},{1,2,3,4}};   
    return 0;
}

But the compiler is throwing some warnings:

 In function 'main':
8:2: error: variable-sized object may not be initialized
  int matrix[r][c] = {{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  ^
8:2: warning: excess elements in array initializer [enabled by default]
8:2: warning: (near initialization for 'matrix[0]') [enabled by default]
8:2: warning: excess elements in array initializer [enabled by default]
8:2: warning: (near initialization for 'matrix[0]') [enabled by default]
......

And then I change the code in this way:

#include <stdio.h>
#include <stdlib.h>

#define R 3
#define C 4


int main(int argc, char const *argv[]){
    int matrix[R][C] = {{1,2,3,4},{1,2,3,4},{1,2,3,4}};
    return 0;
}

And there are no more warnings.

Why the compiler is throwing warnings with the variables?

Miguel
  • 21
  • 4

0 Answers0