简体   繁体   中英

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. (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* .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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