Ask your own question, for FREE!
Mathematics 13 Online
OpenStudy (anonymous):

How does we perform pointer-arithmetic on multidimensional arrays in C? Kudos if you also explain how multidimensional arrays are actually implemented in C.

OpenStudy (anonymous):

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?

hero (hero):

?

OpenStudy (anonymous):

You literally just replace it with pointer arithmetic ... I don't see where your problem is.

OpenStudy (anonymous):

That's just it: I need an example of how it is done in my particular case up there

OpenStudy (anonymous):

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

OpenStudy (anonymous):

simply switch the 0 and the j, and it correctly prints out the 'column'

OpenStudy (anonymous):

...I feel enlightened.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

right; a is effectively a int**

OpenStudy (anonymous):

or am i mistaken?

OpenStudy (anonymous):

Yes, for example, a 2d array simply points to the beginning of a 1d array of pointers.

OpenStudy (anonymous):

I think I'm mistaken.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!