I have fixed your code :
#include <stdio.h> void printMatrix(int matrix[2][3]) { int i = 0, j = 0; for(i = 0 ; i < 2 ; i++) { for(j = 0 ; j < 3 ; j++) { printf("%03d ", matrix[i][j]); } printf("\n"); } printf("\n"); } int main(void) { int matrix[2][3] = {{1, 3, 4}, {2, 0, 1}}; printMatrix(matrix); return 0; }
The code is not robust and beautiful. Mainly, I have fixed your coding style.
Be careful, the last "element" in the following 'for' loop must not increment i, but j !
for(j=0; j<sizeof matrix[0]/sizeof matrix[0][0];i++){
It must be replaced by (or better, by my above code...)
for(j=0; j<sizeof matrix[0]/sizeof matrix[0][0];j++){
EDIT :
You can retrieve the size of your matrix :
leni = sizeof matrix / sizeof matrix[0][0];
sizeof matrix --> 8
sizeof matrix[0][0] --> 4
leni --> 2
lenj = sizeof matrix[0] / sizeof matrix[0][0]
sizeof matrix[0] --> 12
sizeof matrix[0][0] --> 4
lenj --> 3
#include <stdio.h> void printMatrix(int matrix[][3]) { int i = 0, j = 0, leni = 0, lenj = 0; leni = sizeof matrix / sizeof matrix[0][0]; lenj = sizeof matrix[0] / sizeof matrix[0][0]; for(i = 0 ; i < leni ; i++) { for(j = 0 ; j < lenj ; j++) { printf("%03d ", matrix[i][j]); } printf("\n"); } printf("\n"); } int main(void) { int matrix[2][3] = {{1, 3, 4}, {2, 0, 1}}; printMatrix(matrix); return 0; }
void printMatrix(int matrix)you define the argument as anint. It will never, ever have indexes:matrix[i][j]is illegal becausematrixis theintargument