This is a follow up to my previous question. The following simple code to print a matrix works without any errors or warnings at my home computer (GNU GCC compiler):
#include <stdio.h>
#include <stdlib.h>
void printMatrix(int rows, int columns, int matrix[rows][columns]);
void printMatrix(int rows, int columns, int matrix[rows][columns])
{
for (int i = 0; i < rows; i = i + 1)
{
for (int j = 0; j < columns; j = j + 1)
{
printf("%d ", matrix[i][j]);
if (j == columns - 1)
printf("\n");
}
}
printf("\n");
}
int main()
{
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
};
int rows = 3;
int columns = 4;
printMatrix(rows, columns, matrix);
}
However, when I try to use it at another computer (that uses the Clang compiler) I get the error:
||=== Build: Debug in Test (compiler: LLVM Clang Compiler) ===|
...|5|error: variable length array used [-Werror,-Wvla]|
...|5|error: variable length array used [-Werror,-Wvla]|
...|7|error: variable length array used [-Werror,-Wvla]|
...|7|error: variable length array used [-Werror,-Wvla]| ||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===
As I understand it this error is a complaint that "matrix[rows][columns]" uses variable lengths. Could you please tell me how to fix this error so that I can still use "printMatrix" in the same way for arbitrarily large matrices?