I programmed a function which can rotate 4 x 4 arrays, and now i want to output it... But I get a bunch warnings, but it compiles and works, so I guess i only did some "minor/formal" mistakes.
#include <stdio.h>
//-----------------------------------------------------------------------------
///
/// This function prints four 4x4 matrix
///
/// @param m1, m2, m3, m4 input matrix you want to print.
///
/// @return 0
//-----------------------------------------------------------------------------
void *print_matrix(char m1[4][4], char m2[4][4], char m3[4][4], char m4[4][4])
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
printf("%c ", m1[i][j]);
}
printf(" ");
for(int j = 0; j < 4; j++)
{
printf("%c ", m2[i][j]);
}
printf(" ");
for(int j = 0; j < 4; j++)
{
printf("%c ", m3[i][j]);
}
printf(" ");
for(int j = 0; j < 4; j++)
{
printf("%c ", m4[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
//-----------------------------------------------------------------------------
///
/// This function rotates a 4x4 matrix
/// (and collects tears from overworked students @2am, because wtf is )
///
/// @param m, the matrix you want to rotate
///
/// @return m_r the rotated matrix
//-----------------------------------------------------------------------------
char *rotate_matrix(char m[4][4])
{
static char m_r[4][4];
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
m_r[i][j] = m[3-j][i];
}
}
return *m_r;
}
int main(void)
{
char matrix_t [4][4] = {{ '-', '-', '-', '-' },
{ '-', 'O', '-', '-' },
{ 'O', 'O', 'O', '-' },
{ '-', '-', '-', '-' }
};
char matrix_z [4][4] = {{ '-', '-', '-', '-' },
{ '-', 'O', 'O', '-' },
{ 'O', 'O', '-', '-' },
{ '-', '-', '-', '-' }
};
char matrix_l [4][4] = {{ '-', '-', '-', '-' },
{ '-', 'O', '-', '-' },
{ '-', 'O', '-', '-' },
{ '-', 'O', 'O', '-' }
};
char matrix_i [4][4] = {{ '-', '-', 'O', '-' },
{ '-', '-', 'O', '-' },
{ '-', '-', 'O', '-' },
{ '-', '-', 'O', '-' }
};
char (*array)[4][4];
print_matrix(matrix_t, matrix_z, matrix_l, matrix_i);
array = rotate_matrix(matrix_t);
print_matrix(array, matrix_z, matrix_l, matrix_i);
return 0;
}
This are the errors i get:
102:9: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
array = rotate_matrix(matrix_t);
,
103:16: warning: passing argument 1 of ‘print_matrix’ from incompatible pointer type [-Wincompatible-pointer-types]
print_matrix(array, matrix_z, matrix_l, matrix_i);
and
note: expected ‘char (*)[4]’ but argument is of type ‘char (*)[4][4]’
void *print_matrix(char m1[4][4], char m2[4][4], char m3[4][4], char m4[4][4])
And to be honest, i don't get these errors, because the code seems to work, I'm never out of boundaries and obviously I'm not doing any memory violations...