Loading...
Pages: [1] 2   Go Down
Author Topic: Text Array  (Read 548 times)
0 Members and 1 Guest are viewing this topic.
Colorado
Offline Offline
Edison Member
*
Karma: 39
Posts: 1225
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: 21
Posts: 703
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: 39
Posts: 1225
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Danke!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 137
Posts: 19052
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.

France
Offline Offline
God Member
*****
Karma: 19
Posts: 616
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: 313
Posts: 35502
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
Tesla Member
***
Karma: 89
Posts: 6377
-
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

Offline Offline
Newbie
*
Karma: 0
Posts: 27
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
Tesla Member
***
Karma: 89
Posts: 6377
-
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

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
Tesla Member
***
Karma: 89
Posts: 6377
-
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

Offline Offline
Sr. Member
****
Karma: 7
Posts: 388
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
Tesla Member
***
Karma: 89
Posts: 6377
-
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

Offline Offline
Newbie
*
Karma: 0
Posts: 27
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: 313
Posts: 35502
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
Print
 
Jump to: