Text Array

Is there a way to do something like this:

char sNames[7] = {'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sun'};

Serial.print(sNames[1]);

And have it output 'Mon' ?

Seems silly to have to write a switch case to figure out what to print.

char* sNames[] = {
      "Sun"	// 0
    , "Mon"	// 1
    , "Tue"	// 2
    , "Wed"	// 3
    , "Thu"	// 4
    , "Fri"	// 5
    , "Sat"	// 6
};

Serial.print(sNames[1]);

Danke!

Is there a way to do something like this:
Code:

char sNames[7] = {'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sun'};

NB - double quotes and a pointer.

Or,

char sNames[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sun"};
char sNames[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sun"};

On some calendars, Sat comes after Fri.

guix:
Or,

char sNames[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sun"};

What does that actually do? And specifically, what does it do if the number of characters in an initialiser string is not 3?

PeterH:

guix:
Or,

char sNames[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sun"};

What does that actually do? And specifically, what does it do if the number of characters in an initialiser string is not 3?

That would create a matrix like this

0 1 2 3
0 'S' 'u' 'n' '\0'
1 'M' 'o' 'n' '\0'
2 'T' 'u' 'e' '\0'
3 'W' 'e' 'd' '\0'
4 'T' 'h' 'u' '\0'
5 'F' 'r' 'i' '\0'
6 'S' 'a' 't' '\0'

llukkari:

PeterH:
What does that actually do? And specifically, what does it do if the number of characters in an initialiser string is not 3?

That would create a matrix like this

And the second question?

Strings should be null terminated, hence 4 instead of 3.

hart:
Strings should be null terminated, hence 4 instead of 3.

Not sure whether you intended that to answer the question I asked, but it doesn't.

Simply allocate space that is not used. If it would have been declared as array[5] = "abc" it would contain:

0 1 2 3 4
a b c \0 X

where X is whatever junk was there to begin with. However, no you can also use strcpy(array, "abcd") and not worry about it.

KeithRB:
Simply allocate space that is not used.

That's if the initialiser is smaller than the declared size. What about the other case?

PeterH:
And the second question?

Absolutely anything can happen, horrible nondeterministic things eg. nasal demon will fly out.

That's if the initialiser is smaller than the declared size. What about the other case?

There will be an error message from the compiler.

Here is what Harbison and steele say:
"It is not an error - but it might be confusing to a reader - if the string is too long for a character array of specified size. (It is an error in C++)"

So I guess you don't get demons, but I don't know what it actually does. I am guessing it just fills the array with the proper number of characters.

So I guess you don't get demons, but I don't know what it actually does. I am guessing it just fills the array with the proper number of characters.

Why guess? Fire up the IDE and find out.

char sNames[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Saturday"};

void setup() {}
void loop() {}

sketch_jan07a:0: error: initializer-string for array of chars is too long

Is there a string longer or shorter than 3 characters in his code? No so why are you discussing things unrelated to the question...

guix:
Is there a string longer or shorter than 3 characters in his code? No so why are you discussing things unrelated to the question...

One of the criteria I use to select the most appropriate coding technique is how maintainable it is, and how robust in the presence of coding errors. The technique being proposed introduces the possibility of a type of error which I'm not familiar with, so I wanted to find out what the consequences of that type of error would be. If you don't like that, and you feel it's any of your business to decide what should and shouldn't be discussed here, of course you're at liberty to complain to the moderators.

If we're going to throw things out there that don't directly answer the question then I'll post this one -

uint32_t sNames[] = {
      '\0nuS'   // 0
    , '\0noM'   // 1
    , '\0euT'   // 2
    , '\0neW'   // 3
    , '\0uhT'   // 4
    , '\0irF'   // 5
    , '\0taS'   // 6
};

Serial.print((char*)&sNames[1]);

Oh, now I just feel silly ...

EDIT: Then again this is more memory efficient than my original post!