How does we perform pointer-arithmetic on multidimensional arrays in C? Kudos if you also explain how multidimensional arrays are actually implemented in C.
int i, j; int a[][3] = {{4,1,2}, {3,6,5}, {9,9,5}}; /* Prints all of column 1*/ for (j = 0; j < 3; ++j) { fprintf(stdout, "%d\n", a[0][j]); } what if I want to replace a[0][j] with a pointer-arithmetic expression?
?
You literally just replace it with pointer arithmetic ... I don't see where your problem is.
That's just it: I need an example of how it is done in my particular case up there
Since multidimensional arrays are just arrays of arrays, I guess a[0][j] can be replaced by *(*(a + 0) + j); *(a + 0) gets out an array (the first ), and the remainder of the expression gets out the element at index j of that array. actually, now that I've explained it myself, I think that comment is a lie; it prints out all elements of the first row of that matrix :-P
simply switch the 0 and the j, and it correctly prints out the 'column'
...I feel enlightened.
A way to think about it is that in C there isn't really such a thing as an array as implemented in other languages, as an array is simply a pointer. Hope that helps.
right; a is effectively a int**
or am i mistaken?
Yes, for example, a 2d array simply points to the beginning of a 1d array of pointers.
I think I'm mistaken.
Join our real-time social learning platform and learn together with your friends!