Character array initialization.

I backed into a knowledge of Arduino programming, although I have been programming in many other languages, all the back to 1962.

But there is one structure that I hate, and that is the character array. I use String whenever I can, because it comes with a rich set of manipulation functions which are succinct and easy to remember. I see comments now and again about the perils of String, but I've never personally had any difficulties using them.

However, sometimes I just can't avoid the character array, like when I'm using the Radiohead library for radio links (excellent library: far better thought out than Moteino). So there are two things I want to know about the character array:

  1. is there an easy one to set one to empty, or do I have to invoke a do-loop to set all entries to null?
  2. if I actually want to insert a null in a character array as a legitimate information symbol (not a terminator), what will happen?

Thanks in advance for any assistance.

Hello,

Do you really need to empty the char array? Normally there is no reason to do that, you can just set the first element to NULL (mystring[0] = '\0'), then all functions using strings (null terminated char arrays; such as strcmp) will think it's empty. If you really want to empty the whole char array, you can use memset.

You can insert NULL characters where you want (like the strtok function does), but then you will not be able to use functions like strcmp correctly, you could write your own functions, but finding the end of the string will not be easy, if not impossible...

What are you trying to do?

jrdoner:
But there is one structure that I hate, and that is the character array. I use String whenever I can, because it comes with a rich set of manipulation functions which are succinct and easy to remember. I see comments now and again about the perils of String, but I've never personally had any difficulties using them.

Keep writing small code or use bigger Arduinos later on. C++ String copies itself to add a char, that nice easy to type line soaks up cycles and leaves holes in the limited heap but... it's sooo easy to code!

The string.h library has Just As Rich A Set Of Text Manipulation Functions as C++ strings and you don't need special code to get at the text nor do any of the functions shuffle your data around which is nice when you're using pointers to get at it.

C++ Strings are okay on a PC with plenty of RAM to waste. It's like having a sink in the kitchen.
Question is if it's a good idea to fit a kitchen sink on a bicycle which is an UNO compared to a PC house.

However, sometimes I just can't avoid the character array, like when I'm using the Radiohead library for radio links (excellent library: far better thought out than Moteino). So there are two things I want to know about the character array:

  1. is there an easy one to set one to empty, or do I have to invoke a do-loop to set all entries to null?
  2. if I actually want to insert a null in a character array as a legitimate information symbol (not a terminator), what will happen?

Thanks in advance for any assistance.

  1. That's the default for global arrays. There is also the string.h memset() command to set them all fast.
  2. Text manipulation commands see the NULL as a terminator, you'd need to read that with a different function or just operate on the chars of the array as char variables -- since they are.

Wait till you start dealing with char arrays stored in flash....

Thanks, folks,

You've at least convinced me to look up the collection of string manipulation functions. Perhaps I will lose my prejudice.

jrdoner:
Thanks, folks,

You've at least convinced me to look up the collection of string manipulation functions. Perhaps I will lose my prejudice.

If you look at the source code for the String class, you'll see that ALL of it's methods are based on strings, which is the main reason why it is a useless class.

Here's a decent summary of the str* and mem* functions provided in the string.h library:

Every C programmer needs to understand these and especially strtok().

The String class is a crutch. Usually, it bloats the code by at least 1K over the char array alternatives. Also, most of the mem* functions are really fast, as most do no checking on overlapping, type checking, etc...they just scream away at their task. For example, suppose you want to set an array named hyphen[] to the hyphen character. Many programmers use a for loop. Try instead using memset():

memset(hyphen, '-', sizeof(hyphen));

If you're working on a scrolling LCD display, where you need to bump a character off the left and show the new character on the right of the display (assume the array is named msg[]):

memmove(msg, &msg[1], displayWidth - 1);

This function does do some checking for overlapping and handles it properly. memcpy() is likely faster but may not work correctly depending upon how it is implemented.

Write a few String programs, note their sizes, and then replace the String methods with string function calls using char arrays. You should see noticeably smaller program sizes.