Problem with nested for loops for setting values in a 2D Array

Hey All,

First post, but I'll contribute if/when i see the opportunity.

So I bought a couple(4) of 8x8 led matrixes and 2 74ls154 4 to 16 decoders thats all hooked up and working fine, but now my focus turns to what to do with the display, so in wanting to section it out to display different digits, etc but the problem im having is when I attempt to set the values of an array to one that i defined(using a for loop each to select the row and column of the cell that should be written), i get an error no matter how i order it, here's my prototype code

...
int VirtArray1[5][3] = {
{1,1,1},
{1,0,1},
{1,0,1},
{1,0,1},
{1,1,1}};

...
#define zero[5][3] {
{1,1,1},
{1,0,1},
{1,0,1},
{1,0,1},
{1,1,1}
}

...

void test(){

for(int a = 0; a < 5; a++)
{

for(int b = 0; b < 3; b++)
{
if( h0 == 0){ //h0 is just an incrementer for a clock
VirtArray1[a] = zero[a];
** }**
** }**
** }**
** }**
and heres the problem i've been seeing, no matter how i reorder the loops or putting the if on the outside,
ArraynoTimer.cpp: In function 'void test()':
ArraynoTimer:208: error: 'set' was not declared in this scope
ArraynoTimer:208: error: expected `;' before 'VirtArray1'
can anyone advise on what i might be running into here? i can post the full code if need be.
Thanks!

I just don't see how that could work. A #define just does text substitution, so this line:

  VirtArray1[a] = zero[a];

is expanded out to:

   VirtArray1[a] = [5][3] { 
        {1,1,1}, 
        {1,0,1}, 
        {1,0,1},
        {1,0,1},
        {1,1,1}
} [a];

That doesn't make sense, right?

This compiles, at least:

int VirtArray1[5][3] = {
        {1,1,1},
        {1,0,1},
        {1,0,1},
        {1,0,1},
        {1,1,1}};


const int zero[5][3] = { 
        {1,1,1}, 
        {1,0,1}, 
        {1,0,1},
        {1,0,1},
        {1,1,1}
};

int h0;

void test(){
  for(int a = 0; a < 5; a++)
  {

    for(int b = 0; b < 3; b++)
    {
      if( h0 == 0){        //h0 is just an incrementer for a clock
        memcpy (VirtArray1, zero, sizeof VirtArray1);
      }
    }
  }
}

void setup () {}
void loop () {}

There could be other ways of doing it, this method should be fast enough, if slightly obscure.

When you #define a macro you are setting up a text substitution. Your code:

#define zero[5][3] = {{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}};

 void test() {
     for(int a = 0; a < 5; a++) {
          for(int b = 0; b < 3; b++) {
              VirtArray1[a][b] = zero[a][b];
          }
     }
}

gets expanded when the macro is processed into:

void test() {
     for(int a = 0; a < 5; a++) {
          for(int b = 0; b < 3; b++) {
              VirtArray1[a][b] = [5][3] = {{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}};[a][b];
          }
     }
}

As you can see, after the macro substitution this make no syntactic sense. You can't use a #define to create an array. You best bet it to define a const array. Since your values are 0 and 1 you might be able to save a lot of space by using a boolean (0/1, false/true) array. At least use byte rather than storing each bit in a 16-bit integer!

const boolean zero[5][3] = {{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}};

 void test() {
     for(int a = 0; a < 5; a++) {
          for(int b = 0; b < 3; b++) {
              VirtArray1[a][b] = zero[a][b];
          }
     }
}

Hey Guys,

thanks so much, I pulled the #define from an example I saw using an un-multiplexed 8x8 screen using all the pins, didnt even cross my mind before but makes complete sense now.

I know there are probably more elegant ways of going about this but its been a few years since school and I just wanted to dive in and get my sea legs back, thanks again for your help!

Because it seems you only want zero's and ones it might be worth to use ints and use some bit - magic

give it a try

const uint8_t zero[5] = { B00000111, B00000101, B00000101, B00000101, B00000111};  
// only the last 3 bits are used so zero can be seen as an array[8][5];

 void test() {
     for(int a = 0; a < 5; a++) {
          for(int b = 0; b < 3; b++) {
              VirtArray1[a][b] = bitRead(zero[a], b);
          }
     }
}