0

I have this code which works :

String ErrorMsg;
.......
if(!UploadPacketError)
{
    ErrorMsg="OK";
}
else
{
   ErrorMsg="NOK";
}

String creates a dynamically allocated array which I want to avoid. I would prefer something like :

unsigned char ErrorMsg[5];
......
if(!UploadPacketError)
    {
        strcpy(ErroMsg,"OK",3); // +1 for the Null Termination ?
    }
    else
    {
       strcpy(ErroMsg,"NOK",4); // +1 for the Null Termination ?
    } 

i.e. is there a way to dynamically assign a value to a fixed size character array ?

Cheers Thomas

2 Answers 2

1

Almost.

First it's char not unsigned char:

char ErrorMsg[5];`

Second you don't specify a size with strcpy:

strcpy(ErrorMsg, "OK");

There is a variant of strcpy called strncpy which copies at most n characters:

strncpy(ErrorMsg, "OK", 5);

That is used to prevent buffer overruns. But when working with string literals like that it's pretty pointless.

10
  • Thanks Majenko. Is there a way to do do something like : String flashhtml = "<h1>Programming</h1><h2>" + ErrorMsg + " Programming Over " ; Something similar to concatenating multiple fixed strings and variable strings ? I know there are other ways to do this , but i was looking for the simplest possibility.
    – EmbSysDev
    Apr 9, 2020 at 14:46
  • 1
    Into a suitably sized char buffer? Yes. strcpy replaces the string content from the start. strcat concatenates more content on to the end. strcpy(buffer, "<h1>Programming</h1><h2>"); strcat(buffer, ErrorMsg); strcat(buffer, " Programming Over "); ...
    – Majenko
    Apr 9, 2020 at 14:54
  • Terrific , Majenko,exactly what I was looking for !
    – EmbSysDev
    Apr 9, 2020 at 15:07
  • 1
    @EmbSysDev I have a small blog post about replacing String with C strings. majenko.co.uk/blog/evils-arduino-strings
    – Majenko
    Apr 9, 2020 at 15:08
  • Very nice blog indeed. Arduino is a wonderful tool;but it does seem to help you mess up bigtime. I guess like everyhing else, one learns by first messing up
    – EmbSysDev
    Apr 9, 2020 at 15:17
0

Also recomending the fixed char approach but for completeness there is a possibility to "get away with String class" IF you ONLY use fixed AND predefined Strings to build dynamicly variable messages:

bool first = true;

String A = F("My first ");
String B = F("My second ");
String C = F("answer");
if (first) serial.println(A+C);
else serial.println(B+C);

Read more on the F-macro here.
Conclusion:

  • Large blocks of text to a file system (like SPIFFS, LittleFS, SD)
  • dynamic texts to large enough fixed char arrays and
  • short messages (consisting of fixed parts) can be created with the F-Macro
3
  • 1
    Assigning strings A, B and C will allocate heap and copy the data into it wasting RAM. Adding the strings will concatenate them in memory and return the result wasting more RAM. There is never a good reason (or even a good way) to use String.
    – Majenko
    Apr 9, 2020 at 17:23
  • @Majenko: for people with a casual amateur interest in programming, efficiency and performance aren't likely to be driving factors compared to code simplicity and readability. For large projects every bit counts, but using direct port access or cstrings won't improve the perceived performance of a simple 4x20 LCD digital thermometer project, and could very well be harder for the "programmer" to customize than one using digitalWrite(), literals, operators, and String methods. Sometimes "getting away with ___" means "yes, i can mod this" vs "idk what this even means"; have mercy on the noobs.
    – dandavis
    Apr 10, 2020 at 16:26
  • @dandavis I said "good" reason. That is not a good reason. It is a reason, but not good. It's not about efficiency, or performance. It's about someone writing some seemingly simple code and then turning up here some time later saying "My Arduino crashes randomly after a few hours and I can't work out why". And of course they used String. And the heap fragmented, and overflowed.
    – Majenko
    Apr 10, 2020 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.