I have the following code in C
int main(int argc, char *argv[]) {
int row=2, col=3, f,c;
char array[row][col] = { {'A', 'B', 'C'}, {'a', 'b', 'c'} };
for(f=0; f<row; f++) {
for(c=0; c<col; c++)
printf("%c", a[f][c]);
printf("\n");
}
return 0;
}
Why does this line NOT work like this:
char array[row][col] = { {'A', 'B', 'C'}, {'a', 'b', 'c'} };
error: excess elements in array initializer
But why does it DO work like this?
char array[2][3] = { {'A', 'B', 'C'}, {'a', 'b', 'c'} };
Errors(0) Warnings(0)
In C and C++ the size of arrays must be known at compile time. That's why you can't use variables to declare them.
What you can do is define the array and let the compiler infer the size.
So:
Or if you need the size to be dynamic, you should use the heap.