defining and calling functions with multidimentional arrays as arguments
Hello this is my first post, so... please help me.
I want to create a function that prints the content of a matrix(2d array).
I know that in order to set a matrix as a argumentin a function I have to do it like this
(example)
Code:
void printMatrix(matrix[][N], size){
int i,j;
for(i=0;i<size;i++)
for(j=0;j<N;j++)
printf("matrix[%d][%d]",i,j);
}
or something like that... but in this case I am supposed to know the value of N.
and that is my problem. Because i don't know the value of N
I want to create a function that can work on any size of matrix
a friend told me that :
Code:
void printMatrix( *matrix[], size){
int i,j;
for(i=0;i<size;i++)
for(j=0;j<size;j++)
printf("matrix[%d][%d]",i,j);
}
would work fine.... and actually when compiling there is no problem in the declaration, but the problem is in the calling line.
How should i call this function?
(i was calling it
printMatrix(someMatrix,6)
but it doenst work fine)
help.
Help!!, at first I though it was ok, but not....
this is my code:
Code:
#include<stdio.h>
int main(){
int matrix1[5][5]={0,0,0,0,0,0,4,0,0,0,2,0,0,0,3,0,0,0,9,0,4,5,0,0,0};
int matrix2[4][4]={0,5,0,0,6,0,0,0,0,0,0,0,0,0,3,0};
int matrix3[2][2]={0,4,0,0};
printf("calling printMatrix");
printMatrix(matrix1,5);
printMatrix(matrix2,4);
printMatrix(matrix3,2);
return 0;
}
void printMatrix( int **matrix, int size)
{
int i,j;
for(i=0;i<size;i++){
for(j=0;j<size;j++)
printf("%3d",matrix[i][j]);
printf("\n");
}
}
and when compiling
Code:
i60-41-158-186:~/Desktop/tuesday4_2 nacho$ gcc codeTesting.c
codeTesting.c:15: warning: conflicting types for 'printMatrix'
codeTesting.c:7: warning: previous implicit declaration of 'printMatrix' was here
and when executing...
Code:
i60-41-158-186:~/Desktop/tuesday4_2 nacho$ ./a.out
Bus error
i60-41-158-186:~/Desktop/tuesday4_2 nacho$
what to do??