Colorado
Offline
Edison Member
Karma: 41
Posts: 1242
Reviving dead brain cells with Arduinos.
|
 |
« on: January 08, 2013, 12:26:42 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #1 on: January 08, 2013, 12:34:19 am » |
char* sNames[] = { "Sun" // 0 , "Mon" // 1 , "Tue" // 2 , "Wed" // 3 , "Thu" // 4 , "Fri" // 5 , "Sat" // 6 };
Serial.print(sNames[1]);
|
|
|
|
« Last Edit: January 08, 2013, 12:41:45 am by lloyddean »
|
Logged
|
|
|
|
|
Colorado
Offline
Edison Member
Karma: 41
Posts: 1242
Reviving dead brain cells with Arduinos.
|
 |
« Reply #2 on: January 08, 2013, 12:44:17 am » |
Danke!
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: January 08, 2013, 03:35:38 am » |
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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
France
Online
God Member
Karma: 19
Posts: 616
Scientia potentia est.
|
 |
« Reply #4 on: January 08, 2013, 08:09:18 am » |
Or, char sNames[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sun"};
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35511
Seattle, WA USA
|
 |
« Reply #5 on: January 08, 2013, 08:44:20 am » |
char sNames[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sun"}; On some calendars, Sat comes after Fri.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6388
-
|
 |
« Reply #6 on: January 08, 2013, 09:55:27 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #7 on: January 08, 2013, 10:08:16 am » |
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' |
|
|
|
|
« Last Edit: January 08, 2013, 10:11:03 am by llukkari »
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6388
-
|
 |
« Reply #8 on: January 08, 2013, 11:32:44 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Newbie
Karma: 2
Posts: 30
|
 |
« Reply #9 on: January 08, 2013, 11:48:23 am » |
Strings should be null terminated, hence 4 instead of 3.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6388
-
|
 |
« Reply #10 on: January 08, 2013, 02:29:26 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 7
Posts: 389
|
 |
« Reply #11 on: January 08, 2013, 02:47:59 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6388
-
|
 |
« Reply #12 on: January 08, 2013, 03:15:44 pm » |
Simply allocate space that is not used.
That's if the initialiser is smaller than the declared size. What about the other case?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #13 on: January 08, 2013, 03:56:03 pm » |
And the second question?
Absolutely anything can happen, horrible nondeterministic things eg. nasal demon will fly out.
|
|
|
|
« Last Edit: January 08, 2013, 04:01:32 pm by llukkari »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35511
Seattle, WA USA
|
 |
« Reply #14 on: January 08, 2013, 04:23:02 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|