Basic array questions

  1. Do integer arrays require a null character, or is that just for char arrays?
  2. For a character array if there are to be 5 characters do I need to declare it with char may array [6], or will the compiler do that for me?
  1. Just for char arrays
  2. yes, declare 6

best,
Michael

  1. Only char arrays need the '\0' character

  2. If you explicitly tell the compiler the size, you need to add one for the '\0'. If you let the compiler count:

char char_array[] = "This is a string";

It will allocate just enough size for the string, and add the trailing '\0'.

  1. No, not unless you have written some function that needs to iterate over the array without knowing its size. Have you ever seen an example with an array of int? Did they null terminate it?

  2. That depends on the syntax you use. If you initialize it at the same time you declare it then the compiler can see the size and you can let it size the array for you. Otherwise you must provide a size. Perhaps a concrete example of what you want to do would be good here.

Thanks! Very helpful. Karma for all.

char arrays don't need a null character - char arrays that are being used as C strings do. Of course, most char arrays are being used as strings, so the nul terminator is the usual case. But you could have - for instance - a char array containing a rolling buffer where a trailing '\0' would be irrelevant.

PaulMurrayCbr:
char arrays don’t need a null character - char arrays that are being used as C strings do. Of course, most char arrays are being used as strings, so the nul terminator is the usual case. But you could have - for instance - a char array containing a rolling buffer where a trailing ‘\0’ would be irrelevant.

Like, for example, the 64 byte serial buffer.