Pages: [1] 2   Go Down
Author Topic: Text Array  (Read 1169 times)
0 Members and 1 Guest are viewing this topic.
Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a way to do something like this:
Code:
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 Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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 Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Danke!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

France
Offline Offline
God Member
*****
Karma: 34
Posts: 978
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

On some calendars, Sat comes after Fri.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12523
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or,
Code:
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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or,
Code:
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 Offline
Shannon Member
****
Karma: 222
Posts: 12523
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

UK
Offline Offline
Newbie
*
Karma: 2
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12523
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 32
Posts: 1377
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Shannon Member
****
Karma: 222
Posts: 12523
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simply allocate space that is not used.

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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

Pages: [1] 2   Go Up
Jump to: