in my code, I was using string class to make an array to store my menu items
String menu[2] = {{"Menu 1"}, {"Menu2"}};
How do I convert this into char arrays and how do I call them?
Example:
Serial.println(menu[0]);
1 Answer 1
Create them as char arrays like this:
const char* menu[2] = {"Menu 1", "Menu 2"};
and use them like this:
Serial.println(menu[0]);
answered Jun 18, 2020 at 17:58
-
Strictly speaking it's an array of
char*
, not an array ofchar
. And you should handle "Menu 1" as aconst char*
, to allow the compiler inhibit you doing nonsense. And avoidwarning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
DataFiddler– DataFiddler2020年06月19日 14:27:36 +00:00Commented Jun 19, 2020 at 14:27 -
-
It is an array of char*. And each one of those pointers points to a char array, not a String. My point there was to distinguish between char array and String object.Delta_G– Delta_G2020年06月19日 14:59:07 +00:00Commented Jun 19, 2020 at 14:59
-
Agreed. Even the warning text calls "hello" a
string constant
. Butstring
(string literals aka const char array) andArduino String Objects
are so different that we should avoid using the term string completely.DataFiddler– DataFiddler2020年06月20日 11:19:48 +00:00Commented Jun 20, 2020 at 11:19
lang-cpp
const char* menu[2] = {"Menu 1", "Menu2"};