Pages: 1 [2]   Go Down
Author Topic: Text Array  (Read 1200 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 32
Posts: 1394
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

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

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

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

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12579
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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!
« Last Edit: January 08, 2013, 11:49:07 pm by lloyddean » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I would take the original question:

Quote
Is there a way to do something like this:

... as being open to different length names being part of what is asked, with the word "like" there.

I liked reply #1 which did not allocate more memory than was required (if, say, the day names were shorter) and would handle longer names.

However reply #19 is, if I may say without giving offence, silly. At the very least it is non-portable, hard to understand, specific to that particular length of names, and requires a cast.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 66
Posts: 2116
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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!

This is wrong anyway,
multi-char character constants are bound to the size of an integer, therefore you can only use uint16_t on Arduino with 2 chars without truncation.
Logged


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

Quote
I liked reply #1
I liked it too, but I would have liked it even more with a "const" qualifier, to prevent anyone stomping on RAM  smiley-cool
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.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Forgot I had the 32 bit Digilent board attached when tested where it works fine.
Logged

Pages: 1 [2]   Go Up
Jump to: