I am new to C and I don't understand why I am not able to easily populate a 2d array.
Here is my code: one function populates the matrix and the other displays it.
#include <stdio.h>
int matrix[5][5];
int size = 5;
void fill_matrix() {
for(int k=0; k<size; k++) {
for(int l=0; l<size; l++) {
matrix[k][l] = k + l;
}
}
}
void print_matrix() {
for(int k=0; k<size; k++) {
for(int l=0; l<size; l++) {
printf("%d ", *matrix[k,l]);
}
printf("\n");
}
}
int main() {
fill_matrix();
print_matrix();
}
And the result is this
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
Instead of this
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
I know that the answer is probably extremely simple, but I was't able to find a solution after few hours so here I am.