Having trouble using the example on the reference page for string.

From the example at the bottom of this reference page for string:

char *myStrings[] = {"This is string 1", "This is string 2", "This is string 3",
                     "This is string 4", "This is string 5", "This is string 6"
                    };

When I try to do this the compiler says "invalid conversion from 'char' to 'char*' [-fpermissive]" about this line

char *hrs[] = {"12", '1', '2', '3', '4', '5', '6', '7', '8', '9', "10", "11"};

I also need to put the month names into an array like this too. How can I do this?

char *hrs[] = {"12", '1', '2', '3', '4', '5', '6', '7', '8', '9', "10", "11"};

You are trying to put a mixture of constant strings and single characters in the array, hence the error

The example

char *myStrings[] = {"This is string 1", "This is string 2", "This is string 3",
                     "This is string 4", "This is string 5", "This is string 6"
                    };

uses all constant strings

Thanks, I had no idea! Is there a way to have both single and multiple characters in the same array? I’m trying to make the time display and I thought an array like above would solve the problem of having a leading zero on the hours, as well as starting the series with a “12” instead of “0”. I dislike having to make “if” statements for everything, I avoid them when possible. I’m using characters because when I use numbers I get a leading space so the time displays like 3: 24: 15 (which looks even more awkward with a monospaced font).

Put integers in the array and use snprintf() to format them as you want

byte hrs[] = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int n = 0; n < 12; n++)
  {
    char buffer[10];
    snprintf(buffer, 10, "hour : %02d", hrs[n]);
    Serial.println(buffer);
  }
}

void loop()
{
}

EXCELLENT!! I'm back in business, thanks so much :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.