Pass reference/pointer to struct?!

Hi there!

I have a multiple of byte arrays:

static const PROGMEM byte  bytes1[][3] = {
{B00000000, B00010111, B00000000}
,{B00000011, B00000000, B00000011}
,{B00011111, B00001010, B00011111} 

static const PROGMEM byte  bytes2[][4] = {
{B0110000, B00010111, B00000000, B00000000}
,{B00000011, B00001100, B00000001, B00000000}
,{B00000011, B00001100, B00000001, B00000000}
,{B00010011, B00001000, B00011001, B00000000} 

static const PROGMEM byte  bytes3[][2] = {
{B00001100, B00010011}
,{B00010111,B00001100}
,{B00000011, B0000000}
,{, B00001000, B00011001}

I want to swap these byte arrays into this struct (where the array should be “byteArray”)

struct MyBytes {
      int width;
      byte * byteArray;
    };

    MyBytes activeBytes;

with this code:

setByteArray(int theByteArray)
{
  if(theByteArray == 1)
{
 activeBytes.width = 3
 activeBytes.byteArray = bytes1
}
if(theByteArray == 2)
{
 activeBytes.width = 4
 activeBytes.byteArray = bytes2
}
if(theByteArray == 3)
{
 activeBytes.width = 2
 activeBytes.byteArray = bytes3
}

After that I want to use the struct in another method and read back these values stored in PROGMEM (ignore the character - 0x20, it works when I use is without the struct: bitBuffer = pgm_read_byte(&font_5x3[character - 0x20]); without any problems):
```
*draw(char *text)
{
while(*text)
 {
   char character = *text++;
   byte bitBuffer[15];
   for(int i = 0; i < activeBytes.width; i++)
   {
     bitBuffer[i] = pgm_read_byte(&activeBytes.byteArray[character - 0x20][i]);
   }
}

}
_
*_ _*Unfortunately I get this error:*_ _*_
In member function 'void draw(char)’:
error: invalid types ‘byte {aka unsigned char}[int]’ for array subscript
      bitBuffer[i] = pgm_read_byte(&activeBytes.byteArray[character - 0x20][i]);
                                                                    ^
exit status 1
invalid types ‘byte {aka unsigned char}[int]’ for array subscript

```
Any ideas how to accomplish what I want?
Thanks in advance!

Any ideas how to accomplish what I want?

First, you need to understand what the type of the thing you want to point to is.

static const PROGMEM byte  bytes1[][3] = {

This is an array of arrays of bytes. To point to it, the variable type needs to be byte **, not byte *.

Thanks for your reply!

I want to use a struct to point to an array of byte arrays, exactly :slight_smile: I dont want to copy the array to the struct I just want to reference to it, swap the reference to the array of byte arrays when needed and then use it in another method.

In detail: I have a font array which is an array of byte arrays. These have different lengths so I want to use a struct to keep track of this.

When I change the struct to:

struct MyBytes {
      int width;
      byte ** byteArray;
    };

I get this error:

cannot convert 'const byte (*)[96][3] {aka const unsigned char (*)[96][3]}' to 'byte** {aka unsigned char**}' in assignment

at this line:

activeBytes.byteArray = bytes1

You want to point to the address where the array lives.

activeBytes.byteArray = &bytes1;

asuryan:
After that I want to use the struct in another method and read back these values stored in PROGMEM (ignore the character - 0x20, it works when I use is without the struct: bitBuffer = pgm_read_byte(&font_5x3[character - 0x20]); without any problems):
```
*draw(char *text)
{
while(*text)
 {
   char character = *text++;
   byte bitBuffer[15];
   for(int i = 0; i < activeBytes.width; i++)
   {
     bitBuffer[i] = pgm_read_byte(&activeBytes.byteArray[character - 0x20][i]);
   }
}

}*

*_ _*Unfortunately I get this error:*_ _*
In member function 'void draw(char)’:
error: invalid types ‘byte {aka unsigned char}[int]’ for array subscript
      bitBuffer[i] = pgm_read_byte(&activeBytes.byteArray[character - 0x20][i]);
                                                                    ^
exit status 1
invalid types ‘byte {aka unsigned char}[int]’ for array subscript*

*_ _*Any ideas how to accomplish what I want?*_ _*Thanks in advance!*_ _*[/quote]*_ _*The compiler doesn't know how to treat a bye pointer (activeBytes.byteArray) as a two-dimensional array. It can treat it as a 1-dimensional array of bytes but you have to do the two-dimensional indexing math manually:*_ _*
*      bitBuffer[i] = pgm_read_byte(&activeBytes.byteArray[(character - 0x20) * activeBytes.width + i);
_
```*_