I need to print out a 3 by 3 matrix in C. I could think up of 3 different ways of doing it. Are all of them equally optimal or is one method better than the other?
Function 1: Passing an 2darray, using array subscripts
void printMatrix(int m[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
}
Function 2: Passing a dbl pointer, using ptr arithmetic
void printMatrix(int **m)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t", *(*(m+i)+j) );
}
printf("\n");
}
}
Function 3: Passing a dbl pointer, using ptr arithmetic (again)
void printMatrix(int **m)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",**m);
(*m)++;
}
(*m)=(*m)-3;
m++;
printf("\n");
}
}