c - 行优先 3-D 数组地址

标签 c arrays multidimensional-array

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

假设这个行优先数组从基地址 100 开始存储,元素 A[25][4][-1] 存储在哪里? 我的答案是416

下一个问题是: 使用相同的假设,什么元素存储在地址 2000 处?

我该如何解决这样的问题?

最佳答案

我认为这可能会有所帮助。我已将其放入代码块中,以便它能够很好地排列。

/*
 *  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.
 */

关于c - 行优先 3-D 数组地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9850412/

相关文章:

c++ - 如何在字节数组中搜索 "n bits"?

c - OCaml 使用字节码编译器从 C 函数返回未装箱的 float

c - OSX/iOS 上的虚拟内存与 Windows 提交/保留行为

python - 我应该学习什么语言作为 C 语言(及其派生语言)的桥梁

javascript - 在 AsyncStorage 中保存一组项目

c++ - 与整数 vector 相乘并相加

Python 3 - 查找出现在多个列表中的字符串,该列表由函数参数填充

php - 根据另一个数组重新排列多维数组每行中的顺序或元素

python - 为什么我不能用 np.zeros 初始化一个数组,然后将元素更改为数组?

arrays - 如何创建未知大小的多维数组?