如何保存在C 3D数组? [英] How are 3D arrays stored in C?
问题描述
据我了解,在C数组按行优先顺序分配。因此,对于一个2×3阵列
I understand that arrays in C are allocated in row-major order. Therefore, for a 2 x 3 array:
0 1
2 3
4 5
存储在内存中的
0 1 2 3 4 5
但是,如果我有一个2×3×2数组:
However, what if I have a 2 x 3 x 2 array:
0 1
2 3
4 5
和
6 7
8 9
10 11
如何在这些存储在内存中?只是连续这样的:
How are these stored in memory? Is just consecutive like:
0 1 2 3 4 5 6 7 8 9 10 11
或者是一些其他的方式?或者它取决于什么?
Or is it some other way? Or does it depend on something?
推荐答案
所有的尺寸被连续存储在内存中。
All "dimensions" are stored consecutively in memory.
考虑
int arr[4][100][20];
你可以说改编[1]
和改编[2]
(类型为 INT [100] [20]
)是连续的结果
或改编[1] [42]
和改编[1] [43]
(类型为 INT [20]
)是连续的结果
或改编[1] [42] [7]
和改编[1] [42] [8]
(类型 INT
)是连续的。
you can say that arr[1]
and arr[2]
(of type int[100][20]
) are contiguous
or that arr[1][42]
and arr[1][43]
(of type int[20]
) are contiguous
or that arr[1][42][7]
and arr[1][42][8]
(of type int
) are contiguous
这篇关于如何保存在C 3D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!