Go Down

Topic: Text Array (Read 1 time) previous topic - next topic

KirAsh4

Is there a way to do something like this:
Code: [Select]
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.

lloyddean

#1
Jan 08, 2013, 06:34 am Last Edit: Jan 08, 2013, 06:41 am by lloyddean Reason: 1
Code: [Select]

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

Serial.print(sNames[1]);

KirAsh4


AWOL

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.
"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.

guix

Or,
Code: [Select]

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

PaulS

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

On some calendars, Sat comes after Fri.

PeterH


Or,
Code: [Select]

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?
I only provide help via the forum - please do not contact me for private consultancy.

llukkari

#7
Jan 08, 2013, 04:08 pm Last Edit: Jan 08, 2013, 04:11 pm by llukkari Reason: 1


Or,
Code: [Select]

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'


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?
I only provide help via the forum - please do not contact me for private consultancy.

hart

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

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

KeithRB

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.

PeterH


Simply allocate space that is not used.


That's if the initialiser is smaller than the declared size. What about the other case?
I only provide help via the forum - please do not contact me for private consultancy.

llukkari

#13
Jan 08, 2013, 09:56 pm Last Edit: Jan 08, 2013, 10:01 pm by llukkari Reason: 1

And the second question?


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

PaulS

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.

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview