行主要3-D阵列地址

时间:2012-03-24 09:00:42

标签: c arrays multidimensional-array

我有这个三维数组声明:A[10..29][2..6][-1..0].

假设这个行主要数组是从基地址100开始存储的,那么存储了元素A [25] [4] [ - 1]?  我的答案是416

下一个问题是: 使用相同的假设,地址2000中存储了哪些元素?

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我认为这可能会有所帮助。我把它放在一个代码块中,以便排好。

/*
 *  Assuming that the array is laid out row/column/other then...
 *      Row-major           Column-major
 *      A[10][2][-1]        A[10][2][-1]
 *      A[10][2][0]         A[10][2][0]
 *      A[10][3][-1]        A[11][2][-1]
 *      A[10][3][0]         A[11][2][0]
 *      A[10][4][-1]        A[12][2][-1]
 *      A[10][4][0]         A[12][2][0]
 *      A[10][5][-1]        A[13][2][-1]
 *      A[10][5][0]         A[13][2][0]
 *      A[10][6][-1]        A[14][2][-1]
 *      A[10][6][0]         A[14][2][0]
 *
 *  Since the array is 20 rows (29 - 10 + 1) by 5 columns (6 - 2 + 1) by
 *  2 other (0 - (-1) + 1), with each elelment given as size 20, the 
 *  overall array size is 20 * 5 * 2 * 20 = 4000.
 *
 *  The address of element A[25][4][-1], given the array is row-major
 *  begining at 100 is...
 *  Base address                                                          100
 *  Offset index0 = (25 - 10) * ((6 - 2 + 1) * (0 - (-1) + 1)) * 20 =   3,000
 *  Offset index1 =                 (4 - 2) * ((0 - (-1) + 1)) * 20 =      80
 *  Offset index2 =                                (-1 - (-1)) * 20 =       0
 *  Offset for A[25][4][-1]                                         =   3,180
 *
 *  Now that you have the idea, I will leave it to you to determine what element
 *  is at address 2000.  Here is a hint, repeat the above but using substraction.
 */