(我意识到有无数关于 SO 的问题和答案与数组和指针有关,但在浏览了其中的许多问题之后,我仍然没有得到它。)
问题:从数组指针访问值的正确语法是什么?
背景:我正在做的事情非常简单:我定义了一些数组,然后定义了一个指向这些数组的数组(下面的代码)。我这样做是为了在 for..loop 中引用我的“圆”数组。 (我意识到我可以通过只定义一个多维数组来完全跳过指针,但是为了便于编码和在这个阶段快速更改,只定义一次“圆形”数组会很有帮助)。
当我尝试访问数组数据时遇到问题。这符合要求,但数据是垃圾:
for (int i = 0; i < circleArrayLength; i++) {
for (int c = 0; c < 6; c++) {
byte *index = circleArray[i][c]; // WRONG
writeBufferSingle(basePattern[*index][0], basePattern[*index][1], 1);
}
writeScreen();
delay(50);
}
那么,从数组指针访问值的正确语法是什么?
byte circle0[7] = { 0, 1, 40, 44, 43, 42, 41 };
byte circle1[7] = { 1, 2, 39, 45, 44, 41, 40 };
byte circle2[7] = { 2, 3, 38, 46, 45, 40, 39 };
byte circle3[7] = { 3, 4, 37, 47, 46, 39, 38 };
byte circle4[7] = { 4, 5, 36, 48, 47, 38, 37 };
byte* circleArray[][7] = { circle0, circle1, circle2, circle3, circle4}
最佳答案
byte* circleArray[][7] = { circle0, circle1, circle2, circle3, circle4}
这几乎肯定是错误的。 circleArray
不应该是 byte*
的数组数组.
byte circleArray[][7] = { circle0, circle1, circle2, circle3, circle4};
...
byte index = circleArray[i][c];
writeBufferSingle(basePattern[index][0], basePattern[index][1], 1);
关于c++ - 来自数组指针的正确语法访问值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23315017/