0

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]);
  • Sounds like you're looking for menu[0].c_str() – KIIV Jun 18 at 16:57
  • 4
    const char* menu[2] = {"Menu 1", "Menu2"}; – Juraj Jun 18 at 17:03
  • By the way, you can access a string. You call a function. You don't call a string. – Delta_G Jun 18 at 17:59
2

Create them as char arrays like this:

const char* menu[2] = {"Menu 1", "Menu 2"};

and use them like this:

Serial.println(menu[0]);
| improve this answer | |
  • Strictly speaking it's an array of char*, not an array of char. And you should handle "Menu 1" as a const char*, to allow the compiler inhibit you doing nonsense. And avoid warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] – DataFiddler Jun 19 at 14:27
  • Edited for the const. – Delta_G Jun 19 at 14:57
  • 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 Jun 19 at 14:59
  • Agreed. Even the warning text calls "hello" a string constant. But string (string literals aka const char array) and Arduino String Objects are so different that we should avoid using the term string completely. – DataFiddler Jun 20 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.