简体   繁体   English

温度变量未从数组分配正确的值

[英]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. 我正在arduino IDE上编写一个程序,我想在其中交换一些变量。 If you are unaware of arduino, it is essentially written in C++. 如果您不了解arduino,它实际上是用C ++编写的。

A bit of background information: 一些背景信息:

  • Pieces is a 2D array 件是2D阵列
  • Pieces will return a string of "n", "1", "2", ..., "5", or "6" 片段将返回字符串“ n”,“ 1”,“ 2”,...,“ 5”或“ 6”
  • Serial.println(msg) is just a print function Serial.println(msg)只是一个打印功能

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. 第一个值也应为n,但它为空白。 Here is how Pieces was initialised: 以下是Pieces的初始化方式:

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. 问题是数组太大,因此我尝试将其更改为chars而不是字符串,但无法正常工作。

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? 有人可以帮我正确初始化吗?

Since it seems to be a memory-related problem, you should consider using simple char s instead of String s, or char[] if you really need multi-character strings. 由于这似乎是与内存有关的问题,因此,您应该考虑使用简单的char而不是String ,如果确实需要多字符字符串,则可以考虑使用char[]

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 *:

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. 如果您不打算执行任何奇特的字符串操作,则String类对您毫无用处。

By the way, note how conveniently you can leave out the array size when initializing. 顺便说一下,请注意在初始化时可以方便地忽略数组大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM