Go Down

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

KeithRB

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.

PaulS

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: [Select]
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


guix

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

PeterH


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

lloyddean

#19
Jan 09, 2013, 02:58 am Last Edit: Jan 09, 2013, 05:49 am by lloyddean Reason: 1
If we're going to throw things out there that don't directly answer the question then I'll post this one -

Code: [Select]

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!

Nick Gammon


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.
http://www.gammon.com.au/electronics

pYro_65


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

Code: [Select]

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.

AWOL

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

lloyddean

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

Go Up