'Why can´t I write char to String?
#include <Preferences.h>
Preferences preferences;
void setup() {
Serial.begin(115200);
char keyToAdd[15];
String valueToAdd;
keyToAdd = Serial.readString();
valueToAdd = Serial.readString();
preferences.begin("licence",false);
preferences.putString(keyToAdd, valueToAdd);
preferences.end();
}
void loop() {
}
I want a key maybe "room" and want to write on my Serial monitor room test but ...
What I got is this error:
incompatible types in assignment of 'String' to 'char [15]'
Solution 1:[1]
Serial.readString() returns a String object, keyToAdd is an array of 15 characters. There is no automatic conversion from String to a char array.
I don't think you need a char array in this case anyway. Just make keyToAdd a String the same as valueToAdd then use c_str() to get a const char* to pass to putString:
void setup() {
Serial.begin(115200);
String keyToAdd;
String valueToAdd;
keyToAdd = Serial.readString();
valueToAdd = Serial.readString();
preferences.begin("licence",false);
preferences.putString(keyToAdd.c_str(), valueToAdd);
preferences.end();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alan Birtles |
