我有一个可能不那么聪明的问题,我正在和C一起工作,而且有一个问题让我有些疑惑。 我有这段代码:
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;
}
但编译器会抛出一些警告:
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]
......
然后我以这种方式更改代码:
#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;
}
并没有更多的警告。
为什么编译器会抛出变量警告?