Search code examples
c++arraysarduinoarduino-idetemp

Temp variable not assigning right value from array


I am writing a program on the arduino IDE where i want to swap some variables around. If you are unaware of arduino, it is essentially written in C++.

A bit of background information:

  • Pieces is a 2D array
  • Pieces will return a string of "n", "1", "2", ..., "5", or "6"
  • Serial.println(msg) is just a print function

Here is the code:

String temp = Pieces[piece][0];
Serial.println(temp);
Serial.println(Pieces[piece][0]);

The code should return 2 of the same values, but what it actually returns is:

""
"n"

The first value should be n as well, but it is just blank. Here is how Pieces was initialised:

String Pieces[27][6] =
  {
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    {"t", "t", "t", "t", "t", "t"}
  };

The problem is that the array is too large, so i've tried to change it to chars instead of strings, but its not working.

Heres what it looks like now:

char Pieces[27][6] =
 {
    {"n", "n", "n", "n", "n", "n"},
    {"n", "n", "n", "n", "n", "n"},
    ...
    {"t", "t", "t", "t", "t", "t"}
 };

And heres the error its giving me:

error: too many initializers for 'char [6]'

Could someone help me to initialize it properly?


Solution

  • Since it seems to be a memory-related problem, you should consider using simple chars instead of Strings, or char[] if you really need multi-character strings.

    Your initialization would then look like this:

    char Pieces[][6] =
     {
        {'n', 'n', 'n', 'n', 'n', 'n'},
        {'n', 'n', 'n', 'n', 'n', 'n'},
        ...
        {'t', 't', 't', 't', 't', 't'}
     };
    

    and your assignment would be

    char temp = Pieces[piece][0];
    

    In case you need to use strings, use char*:

    char* Pieces[][6] =
        {
            { "n", "n", "n", "n", "n", "n" },
            ...
            { "n", "n", "n", "n", "n", "n" }
        };
    
    char *temp = Pieces[0][1];
    

    If you are not going to perform any fancy string operations, the String class is not useful for you anyhow.

    By the way, note how conveniently you can leave out the array size when initializing.