在一个三维数组索引元素 [英] Indexing elements in a 3-D array
问题描述
可能重复:结果
如何使用数组在C ++?
在以下code片断:
int a[2][3][2]={{{1,2},{9,8},{3,7}},{{2,2},{1,4},{5,4}}};
这将是一个由[i]的答案,一个由[i] [j]时,其中i< = 1和J< = 2
我理解没有问题的一个[I] [J] [K] working.Can请你解释一下如何做上述索引原理?
what will be the answers for a[i],a[i][j] ,where i<=1 and j<=2 I have no problem in understanding the a[i][j][k] working.Can you please explain how does the above indexing works?
推荐答案
您必须记住的一点是,有您正在使用2个不同的对象。
The thing you have to remember here is that the there are 2 different objects you are working with.
a[i][j][k]
和
a[i][j], a[i] and a
首先是一个int,其他3是指针
The first is an int, the other 3 are pointers
相关联的存储的是这样的:
The associated memory's look like this:
a : | a[0] | a[1]|
\/ \______________________
a[] : | a[0][0] | a[0][1] | a[0][2] | a[1][0] | a[1][1] | a[1][2] |
\/ \______________
a[][] : | a[0][0][0] | a[0][0][1] | a[0][1][0] | a[0][1][1] | ...
等等......
And so on...
现在的例子中,你让你做arithmic操作的指针如。 A [1] - 一个[0] = 3
。如果你看看内存相关的机智,你可以看到有2个指针之间3内存位置,因此结果是3。
Now in the example you make you do arithmic operations with pointers eg. a[1] - a[0] = 3
. If you look at the memory associated wit that you can see there are 3 memory locations between the 2 pointers, thus the result is 3.
这篇关于在一个三维数组索引元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!