1

I have this code snippet here:


#define byte uint8_t

byte pins0[11] = {  0,  1,  2, 3,   4,  5,  6,  7, 63, 64, 65};
byte pins1[11] = {  8,  9, 10, 11, 12, 13, 14, 15, 60, 61, 62};
byte pins2[11] = { 16, 17, 18, 19, 20, 21, 22, 15, 60, 61, 62};
byte pins3[11] = { 23, 24, 25, 26, 27, 28, 29, 30, 60, 61, 62};
byte pins4[11] = { 31, 32, 33, 34, 35, 36, 37, 38, 60, 61, 62};
byte pins5[11] = { 39, 40, 41, 42, 43, 44, 45, 46, 60, 61, 62};

byte *segPins[6] = {&pins0,&pins1,&pins2,&pins3,&pins4,&pins5};

The compiler shows an error:

src\main.cpp:15:63: error: cannot convert 'byte (*)[11] {aka unsigned char (*)[11]}' to 'byte* {aka unsigned char*}' in initialization
 byte *segPins[11] = {&pins0,&pins1,&pins2,&pins3,&pins4,&pins5};
                                                               ^
src\main.cpp:15:63: error: cannot convert 'byte (*)[11] {aka unsigned char (*)[11]}' to 'byte* {aka unsigned char*}' in initialization
src\main.cpp:15:63: error: cannot convert 'byte (*)[11] {aka unsigned char (*)[11]}' to 'byte* {aka unsigned char*}' in initialization
src\main.cpp:15:63: error: cannot convert 'byte (*)[11] {aka unsigned char (*)[11]}' to 'byte* {aka unsigned char*}' in initialization
src\main.cpp:15:63: error: cannot convert 'byte (*)[11] {aka unsigned char (*)[11]}' to 'byte* {aka unsigned char*}' in initialization
src\main.cpp:15:63: error: cannot convert 'byte (*)[11] {aka unsigned char (*)[11]}' to 'byte* {aka unsigned char*}' in initialization
*** [.pio\build\nanoatmega328\src\main.cpp.o] Error 1

In my opinion, segPins is an Array of byte pointers. So it should pass the compiler but it sees a difference?

brj
  • 375
  • 2
  • 11
Meeresgott
  • 431
  • 3
  • 17
  • shouldn't segPins be an array of ptrs to an array of 11 bytes? – doug Nov 03 '19 at 20:31
  • `byte *segPins[11]` this way ? – Meeresgott Nov 03 '19 at 20:37
  • `byte *segPins[6] = {pins0,pins1,...};` &pins0 is a address of the pointer which is pointing on array of 11 elements and can be bind to `byte**ptr = &pins0`. PS: Note I'm talking now about the case when array decay to pointer but u should understand general situation) – Roout Nov 03 '19 at 20:42
  • 1
    No. `byte (*segPins[6])[11]`; However, brj's answer is simpler and more typically how this should be done. – doug Nov 03 '19 at 20:43
  • Why using the pre-processor? use `type-alias` is safer. `using byte = uint8_t;` or `typedef`. – Itachi Uchiwa Nov 03 '19 at 21:00

1 Answers1

2

What you need is

byte *segPins[6] = {pins0,pins1,pins2,pins3,pins4,pins5};

This is because &pins0 refers to the address of the array pins0.

Edit

For more information see answers to this question.

brj
  • 375
  • 2
  • 11