Hey guys, I’ve run into a bit of a snag while learning C. I was looking at some source code (shown below) and noticed a function call that passes a pointer. However, the function receiving this pointer declares it as a two-dimensional array rather than as a pointer (like `int *name`). This is where I’m stuck—why declare it as a 2D array instead of as a pointer, the way you would for a 1D array?
#include <stdio.h>
// Function that displays a 2D array
// The number of columns (3) must be known at compile time
void afficherMatrice(int mat[][3], int lignes) {
for (int i = 0; i < lignes; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
// Alternative with both dimensions specified
void initialiserMatrice(int mat[2][3]) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
mat[i][j] = i * 10 + j;
}
}
}
int main() {
int matrice[2][3] = {{1, 2, 3}, {4, 5, 6}};
afficherMatrice(matrice, 2);
initialiserMatrice(matrice);
printf("\nAfter initialization:\n");
afficherMatrice(matrice, 2);
return 0;
}
(I think there is an error in this code)