A 2-D array is... a 1-D array of 1-D arrays, and you can use the common pointer arithmetics on both.
That means that you can simply do:
#include <stdio.h>
int main() {
int dar[2][3] = {{1,2,3},
{4,5,6}};
for (int i = 0; i < 2; i++) {
int *bar = *(dar + i); // pointer to the row
for (int j = 0; j < 3; j++) {
printf("%d ",*(bar+j)); // access the values
}
printf("\n");
}
printf("\n");
}
It works, because in dar + i
, dar
decays to a pointer to its first row, so *(dar + 1)
(which is *by definition dar[i]
) represents the i-th row and in turn decays to a pointer to the first element of that row.
Disclaimer: this is just an addition to JJcopl's answer, but too rich to fit in a comment...