3

I wish to define a list of months names and access it when printing a report.

Array was defined as follows:

const char months []= {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};

for (int i=0; i<=11; i++) {

                                 Serial.print(months[i]);
                        }

Tries:

I was looking for help under Arrays in Arduino documentation didn't give, as far as I found, any explanation for such case, but arrays of int

Appreciate any help.

guyd
  • 1,033
  • 2
  • 16
  • 51
  • 3
    I just want to point out that Arduino uses C++. Straightforward questions like this, about how to create and access arrays can be answered by doing a C++ tutorial. There is nothing particularly special about the fact that the code is running on an Arduino. – Nick Gammon Nov 30 '18 at 20:06
  • @NickGammon - thank you for your answer. I'm aware of that, but still I did not find any explanation for such case. – guyd Nov 30 '18 at 20:10
  • 1
    Very good. I try to define and access this list without any success - I suggest for future questions you are more explicit about the problem. I see that I can actually define the array the way you did it without error, but I can see that you would have problems printing it because you used single quotes rather than double quotes. It helps to explain what "without success" actually means. If I said "I tried to bake a cake, without success" how would you advise me? A different recipe? A different technique? A better oven? Better ingredients? – Nick Gammon Dec 01 '18 at 05:49
  • I'll rewrite it. – guyd Dec 01 '18 at 05:55
  • @NickGammon - question is now re-written. Hope it is more undrestandable and not "off-topic" – guyd Dec 01 '18 at 06:01
  • Very good. I see that there are vote(s) to re-open the question. Most of the moderation here is done by community members, so we'll see what happens to it. – Nick Gammon Dec 02 '18 at 05:45

1 Answers1

6

You are almost there:

  • Use a char* instead of char, a char can only contain one character
  • Use " instead of ' (Thanks Juraj)

Then you get:

const char* months[] =
 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Jot
  • 3,246
  • 1
  • 13
  • 21
Michel Keijzers
  • 12,954
  • 7
  • 40
  • 56