简体   繁体   English

来自数组指针的正确语法访问值?

[英]proper syntax access values from an array pointer?

(I realize there aa zillion questions and answers on SO pertaining arrays and pointers but after wading through many of them, I'm still not getting it.) (我意识到关于数组和指针的问题和解答不计其数,但是经过大量的讨论之后,我仍然没有得到答案。)

Question: What is the proper syntax access values from an array pointer? 问题:来自数组指针的正确语法访问值是什么?

Background: What I am doing is very simple: I'm defining some arrays and then defining an array which points to those arrays (code below). 背景:我正在做的事情非常简单:我定义了一些数组,然后定义了一个指向这些数组的数组(下面的代码)。 I am doing this so that I can reference my 'circle' arrays in a for..loop. 我这样做是为了可以在for..loop中引用“圆形”数组。 (I realize I can skip pointers altogether by just defining a multidimensional array but for ease of coding and fast changes at this stage, it would be helpful to define the 'circle' arrays just once). (我意识到只定义一个多维数组就可以完全跳过指针,但是为了便于编码和在此阶段快速更改,只定义一次“圆形”数组将很有帮助)。

I am running into problems when I try to access the array data. 尝试访问数组数据时遇到问题。 This complies but the data is garbage: 这符合,但数据是垃圾:

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);
}

So then, what is the proper syntax access values from an array pointer? 那么,数组指针的正确语法访问值是什么?


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} 

This is almost certainly wrong. 几乎可以肯定这是错误的。 circleArray should not be an array of arrays of byte* . circleArray不应是byte*数组的数组。

byte circleArray[][7] = { circle0, circle1, circle2, circle3, circle4};

... ...

byte index = circleArray[i][c];

writeBufferSingle(basePattern[index][0], basePattern[index][1], 1);

Also, consider storing circle<n> and circleArray in flash instead. 另外,请考虑改为circle<n>circleArray存储在闪存中

There are two method to do that. 有两种方法可以做到这一点。

Method one: 方法一:

#include <stdio.h>

typedef char byte;

int main(void)
{
    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[] = { circle0, circle1, circle2, circle3, circle4};

    size_t circleArrayLength = sizeof(circleArray)/sizeof(circleArray[0]);

    for (size_t i = 0; i < circleArrayLength; i++) {
        for (size_t c = 0; c < 7; c++) {
            printf("circleArray[%zu][%zu] = %d\n", i, c, circleArray[i][c]);
        }
    }
}

Method Two: 方法二:

#include <stdio.h>

typedef char byte;

int main(void)
{
    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};

    size_t circleArrayLength = sizeof(circleArray)/sizeof(circleArray[0]);

    for (size_t i = 0; i < circleArrayLength; i++) {
        for (size_t c = 0; c < 7; c++) {
            printf("circleArray[%zu][%zu] = %d\n", i, c, (*circleArray[i])[c]);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM