I'm having trouble understanding why my code crashes when accessing the array. When converting the function param to int[3][3]
instead of int**
I get no problems, but I can't understand why, since the int**
I'm given as a argument is a valid pointer that points to local memory defined in main
typedef struct {const unsigned int m; const unsigned int n;} mat_size;
void print_mat(int** mat, mat_size s){ // <-- faulty version
int i,j;
for(i = 0; i < s.m; i++){
for(j = 0; j < s.n; j++){
printf("%d ", mat[i][j]);
}
printf("\n");
}
printf("=============");
}
int main(int argc, char** argv) {
int matrix[3][3] = {{1,4,2},{7,-1,15},{33,-10,-1}};
mat_size s = {3,3};
print_mat(matrix, s);
while(1);
return 0;
}
void print_mat(int mat[3][3], mat_size s){ // <-- good version
int i,j;
for(i = 0; i < s.m; i++){
for(j = 0; j < s.n; j++){
printf("%d ", mat[i][j]);
}
printf("\n");
}
printf("=============");
}