简体   繁体   English

使用Arduino和C ++声明和写入数组的问题

[英]Issues with declaring and writing to arrays with Arduino and C++

I'm messing around with an Arduino board for the first time. 我第一次搞乱了Arduino板。

I have an array declared like this (I know don't judge me), it's for storing each character of the LCD as a sort of cache: 我有一个像这样声明的数组(我知道不要判断我),它是用于存储LCD的每个字符作为一种缓存:

char* lcd_characters[] = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};

Then later on I'm trying to write to a specific slot of the array, like this, to save that letter to it: 然后我会尝试写入数组的特定插槽,就像这样,将该字母保存到它:

new_char = String(message.charAt(i));

...blah blah blah... ...等等等等等等...

lcd_characters[pos] = new_char; << error here

However it is giving me this error: 但它给了我这个错误:

error: cannot convert 'String' to 'char*' in assignment

The funny thing is when I do this (below) it do assign the letter to it, however I have a var which is a single letter but can't assign it. 有趣的是当我这样做(下面)它确实给它分配了字母,但是我有一个var,它是一个单独的字母,但不能分配它。

lcd_characters[pos] = "H";

Can someone help me out please. 有人可以帮帮我吗。 Thanks. 谢谢。 I'm brand new to C and been ok so far. 我是C的新手,到目前为止还不错。

Basically I want an array of characters and then I want to write on the array positions with a new value. 基本上我想要一个字符数组然后我想用新值写在数组位置。

Why does it even matter what type of string I'm writing to that array position, I should be able to write a number or boolean there too and call it later. 为什么我正在写入该数组位置的字符串类型,我应该能够在那里写一个数字或布尔值并稍后调用它。 Is there something wrong with the way the array is declared initially? 最初声明数组的方式有问题吗?

Edit: 编辑:

I tried... 我试过了...

lcd_characters[pos] = new_char.c_str(); 

however that's giving me the similar error: 然而,这给了我类似的错误:

invalid conversion from 'const char*' to 'char'

Wtf? 跆拳道? All I want to do is say this array position equals this new value. 我想要做的就是说这个数组位置等于这个新值。 That's it. 而已。 I've done this a million times in javascript, ruby, python (even php) etc. I just want to go, this array... x[12] equals my letter in new_char !!!! 我已经在javascript,ruby,python(甚至php)等中完成了这一百万次。我只想去,这个数组...... x [12]等于我在new_char中的字母!!!! Ahh. 啊。

A few remarks: 几点评论:

  1. Are you using C or C++? 你在使用C或C ++吗? String is a C++ class, but you are creating a an array of c strings ( char * ). String是一个C ++类,但您正在创建一个c字符串数组( char * )。

  2. You are creating an array of strings ( char* var[] equals to char** ), but your naming suggests you want an array of characters. 您正在创建一个字符串数组( char* var[]等于char** ),但您的命名建议您需要一个字符数组。 A c string is basically an array of characters, so stick with that ( char * or char [] ). 一个c字符串基本上是一个字符数组,所以坚持使用它( char *char [] )。

I would recommend you go for only C code in this case: 在这种情况下,我建议你只使用C代码:

char lcdChars[4] = {' ', ' ', ' ', ' '}; // init with spaces
lcdChars[2] = 'x'; // write x to position 3

Note: A string in C++ can output a C string ( char * ) by calling stringInstance.c_str() . 注意:C ++中的字符串可以通过调用stringInstance.c_str()输出C字符串( char * stringInstance.c_str()

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

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