String array length

So, I’ve got a program working better than I expected, except for one minor detail; I’ve got a variable length string array, on which a small loop is dependant. The loop essentially goes over every string in the string array, until it’s worked with all strings. As an example:

String examplestring[] = {
      "string 1",
      "string 2",
      "string 3",
      "string 4"};

However, now the loop working on the strings is running into a small snag:

for (int x = 0;x<sizeof(examplestring);x++){//do some stuff}

This will only ‘do some stuff’ on strings one and two. If I look at the value of sizeof(examplestring), I see 2. Not 4.

sizeof() counts bytes, I think, but I also seem to remember using it before without a hitch.
My question is thusly: is this normal behaviour? If so, how can I setup a loop to go over all the strings in an array of strings?

The program is currently working with the string array having a length consistent with the maximum length, and it overwrites everything with “IGNORE” after every iteration of loop(), and skips fields that still have “IGNORE” in them when it comes across them in the loop working on the strings, but this is not a pretty solution.

is this normal behaviour?

Yes.

how can I setup a loop to go over all the strings in an array of strings?

for (int x = 0;x<numberOfStrings;x++){
  //do some stuff with examplestring[x] 
    
  }

You mean specifying the number of relevant values directly? That would work, but the problem is that the number of relevant values varies highly. Right now, I'm working with a 'keep going until you find a non-relevant value' bit, which also should work, but has the potential to eat up quite a bit of code, and is quite far from an ideal solution.

However, if there's no way to find out the number of strings in a string array, it would seem that that's the best option...

sizeof(examplestring) returns the size of the variable "examplestring". "examplestring" is a pointer to a String object, and thus is 2 bytes (as pointers on the Arduino are 2 bytes - 16 bits).

However, I wonder if you are really going about the whole thing in the right way?

Anything of "variable size" on a small microcontroller is generally to be frowned upon, as you don't have much memory to play with. If at all possible you should really strive to keep everything fixed and countable, so a fixed size array with a limit on the number of entries it can hold, and fixed length char arrays instead of String objects.

In most situations where I don't know how many elements an array holds, etc, is to have a "terminating" element as the final one, for example a final entry with a value of NULL. When you hit the NULL you know you are at the end of the array.

And don't worry about it taking too long - you don't have enough memory to store enough values for it to take more than the blink of an eye to scan them all.

You cannot have more strings in an array than you have allowed for when declaring it and you cannot dynamically expand an array so you will always have an upper limit to search to.

UKHeliBob: You cannot have more strings in an array than you have allowed for when declaring it and you cannot dynamically expand an array so you will always have an upper limit to search to.

You can use malloc(), free(), realloc(), etc but I really wouldn't recommend that on a small microcontroller. One of the bigger ones with at least 128K of RAM, maybe, but not something like a 328P...

There are a couple of ways of finding out how many elements are in an array. Take this code:

String examplestring[] = {
      "string 1",
      "string 2",
      "string 3",
      "string 4"};
      
// number of items in an array
#define NUMITEMS(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))

template< typename T, size_t N > size_t ArraySize (T (&) [N]){ return N; }

void setup ()
  {
  Serial.begin (115200);
  Serial.println (NUMITEMS (examplestring));
  Serial.println (ArraySize (examplestring));
  }  // end of setup

void loop () { }

Output:

4
4

So, both methods work. :slight_smile:

UKHeliBob: You cannot have more strings in an array than you have allowed for when declaring it and you cannot dynamically expand an array

I've been locally declaring the string over and over and over again. And when I think about it, it's a miracle I haven't had problems with that obscene method. After messing around a bit, I've decided my approach was wrong, and ended up with a sort of maximum number of strings. That part now works as intended.