I'd like to define a function that can process a matrix in a flexible way. That is, I need a function that can deal with matrices of any size.
My first try was to deal with the matrix as a pointer, in this way:
#include <stdio.h>
#include <stdlib.h>
#define MAX_L 10
#define MAX_C 10
void initializeMatrix(int *matrix, int lines, int columns){
int i,j,color;
for(i=0;i<lines;i++){
for(j=0;j<columns;j++){
*(matrix+i*columns+j) = rand()%10;
}
}
}
void printMatrix(int *matrix, int lines, int columns){
int i,j,color;
for(i=0;i<lines;i++){
for(j=0;j<columns;j++){
printf("%d\t",*(matrix+i*columns+j));
}
printf("\n");
}
}
int main(){
int x,y,i,color,count,finish;
int matrix[MAX_L][MAX_C];
initializeMatrix(matrix,MAX_L,MAX_C);
printMatrix(matrix,MAX_L,MAX_C);
return 0;
}
Is there any problem in working with matrices in this way? Is it a good practive?