3D 数组如何存储在 C 中? [英] How are 3D arrays stored in C?

查看:52
本文介绍了3D 数组如何存储在 C 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 C 中的数组是按行优先顺序分配的.因此,对于 2 x 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 x 3 x 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];

你可以说arr[1]arr[2](int[100][20]类型)是连续的
或者 arr[1][42]arr[1][43](int[20] 类型)是连续的
或者 arr[1][42][7]arr[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

这篇关于3D 数组如何存储在 C 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆