hi everyone

I wanna to ask about calculation of 2-D array address I remember that equation that can do this

for an array, char a[R][C];

Address of an element a[N][M]; = baseAddress + elementSize * (N*C+ M);

Is it right?? :)

Other thing, is it deferent if the array is lower or upper triangular, or the same way by substituting missing elements by zeros??
e.g.

1 15 20
3 30
60

Any help I would appreciate ..

thanks:)

2
Contributors
1
4
Views
10 Years
Discussion Span
Last Post by Narue

>Is it right??
In theory. In practice it's more like this:

``*( *( baseAddress + N ) + M )``

Why? Because a two dimensional array isn't required to be strictly contiguous in memory. Pick any teacher and they'll tell you that the storage for a two dimensional array is just a one dimensional array that uses special subscripting rules like the one you posted. And that's true, to a point. The elements of an array are required to be contiguous, but at the boundaries of a one dimensional array, all bets are off.

It's because of this boundary rule that you can't portably treat a multidimensional array as a single dimensional array and use a mathematical formula to index it. So while the N*C+M example helps one to understand how multidimensional arrays work, it's not something you should be using in your code.

>Other thing, is it deferent if the array is lower or upper triangular, or
>the same way by substituting missing elements by zeros??
An array is an array. How you use the indices to represent your data doesn't change that.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.