Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How would i go about creating an array of variables such as.....

int incColor, c0, c1, c2, c3, c4, c5 = 0;
int circleArray[11][9] = { {c5, c5, c5, c5, c5, c5, c5, c5, c5}, 
                             {c4, c4, c4, c4, c4, c4, c4, c4, c4}, 
                             {c4, c3, c3, c3, c3, c3, c3, c3, c4}, 
                             {c4, c3, c2, c2, c2, c2, c2, c3, c4}, 
                             {c4, c3, c2, c1, c1, c1, c2, c3, c4}, 
                             {c4, c3, c2, c1, c0, c1, c2, c3, c4}, 
                             {c4, c3, c2, c1, c1, c1, c2, c3, c4}, 
                             {c4, c3, c2, c2, c2, c2, c2, c3, c4}, 
                             {c4, c3, c3, c3, c3, c3, c3, c3, c4}, 
                             {c4, c4, c4, c4, c4, c4, c4, c4, c4}, 
                             {c5, c5, c5, c5, c5, c5, c5, c5, c5} };

So that later on in the code i will be able to address them like this...

void circle(uint16_t mode) 
{
  switch (mode)
  {
     case 0:
     c0 = random(255);
     break; 
     case 1:
     c0 = incColor;
     incColor++;
     break;
  }
  for (int x = 0; x < 11; x++)
  {
     for (int y = 0; y < 9; y++)
    {
       strip.setPixelColor(x, y, Wheel(circleArray[x][y]));
    } 
  }
  c5 = c4;
  c4 = c3;
  c3 = c2;
  c2 = c1;
  c1 = c0;

}

The code above doesnt work, i tested both c5 and circleArray[0][0]

c5 = 34
circleArray[0][0] = 0

circleArray[0][0] should be the same as c5 is what i thought but for some reason the value is not getting set...

Does anybody know what im doing wrong here?

~~~~~~~~~~~~~~EDIT~~~~~~~~~~~~~~~~~

Fixed!! thanks to @sj0h for helping me see a much easier solution so now i can turn this...

int c[6] = {0, 0, 0, 0, 0, 0};    
int  circleArray[11][9] = {  {5, 5, 5, 5, 5, 5, 5, 5, 5},
                             {4, 4, 4, 4, 4, 4, 4, 4, 4}, 
                             {4, 3, 3, 3, 3, 3, 3, 3, 4}, 
                             {4, 3, 2, 2, 2, 2, 2, 3, 4}, 
                             {4, 3, 2, 1, 1, 1, 2, 3, 4}, 
                             {4, 3, 2, 1, 0, 1, 2, 3, 4}, 
                             {4, 3, 2, 1, 1, 1, 2, 3, 4}, 
                             {4, 3, 2, 2, 2, 2, 2, 3, 4}, 
                             {4, 3, 3, 3, 3, 3, 3, 3, 4}, 
                             {4, 4, 4, 4, 4, 4, 4, 4, 4}, 
                             {5, 5, 5, 5, 5, 5, 5, 5, 5} };

void circle(uint16_t mode) 
{
  switch (mode)
  {
     case 0:
     c[0] = random(255);
     break; 
     case 1:
     c[0] = incColor;
     incColor++;
     break;
  }
  for (int x = 0; x < 11; x++)
  {
     for (int y = 0; y < 9; y++)
    {
       strip.setPixelColor(x, y, Wheel(c[circleArray[x][y]]));
    } 
  }
  c[5] = c[4];
  c[4] = c[3];
  c[3] = c[2];
  c[2] = c[1];
  c[1] = c[0];

}

into the real life application....

www.famousmods.com for more info

share|improve this question

2 Answers 2

up vote 3 down vote accepted

References may do what you want:

int &circleArray[11][9] = ......

This will make circleArray an array of references rather than copied values.


Edit: above isn't supported by the standard, and support is compiler dependent.

Using pointers instead, you would have

int *circleArray[11][9] = {{&c5, &c5, .....

and then change all the circleArray acceses to be *circleArray[x][y] instead of circleArray[x][y].


A different approach, which may be more versatile, would be to keep colour array indices in circleArray:

int incColor;
int cn[6] = {0,0,0,0,0,0};
int circleArray[11][9] = { {5,5,5,5,5,5,5,5,5},
                           {4,4 .....

Then c0 would become c[0] and you would access the array value like

cn[circleArray[x][y]]

you could also make circleArray uint8_t to save some space, as all entries will fit inside a byte.

share|improve this answer
    
I tried this and *circleArray[11][9] and the error i got was C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Print.h:63: note: size_t Print::print(long unsigned int, int) <near match> –  Hockeyman271 Feb 28 at 1:16
    
If you use *circleArray[11][9] it will achieve a similar thing, but you will need to de-reference the pointer each time you access circleArray as a value, by putting the star out the front. Where was the error when you used &circleArray ? –  sj0h Feb 28 at 1:24
    
Test1:8: error: declaration of 'circleArray' as array of references it wont let me verify the program, it crashes on the declaration of the array –  Hockeyman271 Feb 28 at 1:41
    
Ok, so it seems that arrays of references are not supported by the C++ standard, but some compilers allow them. In that case, you are back to pointers. I'll edit the answer to show the pointer method. –  sj0h Feb 28 at 1:52
1  
doing it that way may also help other code, like your series of assignments at the end can now be done in a for loop, ie for(int i=5;i>0;--i) c[i]=c[i-1]; –  sj0h Feb 28 at 5:34

circleArray[0][0] won't be the same as c5, because on assignment a value is copied from one variable to another.

To alias one location to multiple variables, you should use pointers or references.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.