A textbook I recently read discussed row major & column major arrays. The book primarily focused on 1 and 2 dimensional arrays but didn’t really discuss 3 dimensional arrays. I’m looking for some good examples to help solidify my understanding of addressing an element within a multi-dimensional array using row major & column major arrays.
+--+--+--+ |
/ / / /| |
+--+--+--+ + | +---+---+---+---+
/ / / /|/| | / / / / /|
+--+--+--+ + + | +---+---+---+---+ +
/ / / /|/|/| | / / / / /|/|
+--+--+--+ + + + | +---+---+---+---+ + +
/ / / /|/|/|/| | / / / / /|/|/|
+--+--+--+ + + + + | +---+---+---+---+ + + +
/ / / /|/|/|/|/ | |000|001|002|003|/|/|/|
+--+--+--+ + + + + | +---+---+---+---+ + + +
|00|01|02|/|/|/|/ | |004|005|006|007|/|/|/|
+--+--+--+ + + + | +---+---+---+---+ + + +
|03|04|05|/|/|/ | |008|009|00A|00B|/|/|/
+--+--+--+ + + | +---+---+---+---+ + +
|06|07|08|/|/ | |00C|00D|00E|00F|/|/
+--+--+--+ + | +---+---+---+---+ +
|09|0A|0B|/ | |010|011|012|013|/
+--+--+--+ | +---+---+---+---+
arr[5][3][4] | arr[3][4][5]
NOTE: Original question incorrectly represented arr[3][4][5]. I have learned that the original subscript represents depth. The data has been corrected to reflect intended array representation.
Example hex data
+---+---+---+---+ +---+---+---+---+ +---+---+---+---+
|000|001|002|003| |100|101|102|103| |200|201|202|203|
+---+---+---+---+ +---+---+---+---+ +---+---+---+---+
|004|005|006|007| |104|105|106|107| |204|205|206|207|
+---+---+---+---+ +---+---+---+---+ +---+---+---+---+
|008|009|00A|00B| |108|109|10A|10B| |208|209|20A|20B|
+---+---+---+---+ +---+---+---+---+ +---+---+---+---+
|00C|00D|00E|00F| |10C|10D|10E|10F| |20C|20D|20E|20F|
+---+---+---+---+ +---+---+---+---+ +---+---+---+---+
|010|011|012|013| |110|111|112|113| |210|211|212|213|
+---+---+---+---+ +---+---+---+---+ +---+---+---+---+
slice 0 slice 1 slice 2
short Arr[3][4][5]; // assume array is filled with hex test data
arr[1][2][3] = 0x10B use slice 1, row 2, col 3
arr[2][3][4] = 0x210 use slice 2, row 3, col 4
resolves to row 4, col 0
row major
{000,001,002,003,004,005,006,007,008,009,00A,00B,00C,00D,00E,00F,010,011,012,013,
100,101,102,103,104,105,106,107,108,109,10A,10B,10C,10D,10E,10F,110,111,112,113,
200,201,202,203,204,205,206,207,208,209,20A,20B,20C,20D,20E,20F,210,211,212,213}
column major
{000,004,008,00C,010,001,005,009,00D,011,002,006,00A,00E,012,003,007,00B,00F,013,
100,104,108,10C,110,101,105,109,10D,111,102,106,10A,10E,112,103,107,10B,10F,113,
200,204,208,20C,210,201,205,209,20D,211,202,206,20A,20E,212,203,207,20B,20F,213}
Calculation offset for arr[1][2][3] using row major offset? Calculation offset for arr[1][2][3] using column major offset?
When I asked this question I was hoping to find some good 3 dimensional array examples. Especially code examples. Since I didn’t find anything that was understandable, I decided to create a little C program to help display the concept. It uses the same test data in a 3x4x5 array. It also includes test data for a 5x5x5 array. It creates a column major array from the row major array so the offset calculations can be verified.
The array offset methods are:
I added comments in the code where applicable to help clarify what the code is doing.