How to find number of "rows" in an array of strings?

I am trying to make a for loop run only for the number of strings there are in an array. I tried to use sizeof but it doesn’t really work. So if an array had 6 sentences, I would want the function to return a 6, but if it has 4 it would return a 4.

The number of strings and the size of the strings can change. Here is an example code:

char* myString[]={"string 1","string 2","string 3","string 4"};

void setup(){
  Serial.begin(9600);  
}

void loop() {
int number;
number=sizeof(myString);  //should return 4 in this case, 3 if there were three strings in array, etc.

for(int i=0; i<number; i++){
  Serial.println(myString[i]);
}
  
delay(1000);
}

This is what the serial monitor is outputting:

string 1
string 2
string 3
string 4

8

string 1
string 2
string 3
string 4

8

but I want it to start at 1 and stop on 4. Any Ideas? I cant just hard code in 4 because the number of strings is variable

Thanks!

You probably need a NULL as a sentinal value, and stop processing once you reach it.

char * myString = {"s1", "s2", "s3"...NULL};
if (myString[i] = NULL)
{
    //Abandon Ship!
}

if (myString[i] = NULL)NO !

I don't want to add anything to the arrays, and I'm not sure I trust that method, no offense.

Is there no function that just reads how many strings are in an array?

Rats, darn typo, it should be

if (string[i] == NULL)...

Try this

  number = (sizeof(myString) / sizeof(myString[0]));

You could explicitly make it an array of pointers:

char *string1 = "a";
char *string2 = "b";
char *string3 = "c";

char *mystring[] = {string1, string2, string3};

Then the sizeof(mystring)/sizeof(mystring[0]) idiom will work.

Sizeof does not return the number of arrays, but rather the size of one array

http://arduino.cc/en/Reference/Sizeof

I’m pretty sure that Sizeof should count the null operator in char myString {“String 1”} defined with quotation marks and Sizeof should return 9 in your case. If its returning 8 it must be due to the use of the pointer *.

sizeof() returns the total number of bytes used by the array. In the code posted by the OP it is an array of pointers, so the total size divided by the size of one array element gives the number of elements in the array.

You're compiling the array. The number of Strings in the array is part of your source. Wouldn't it be easier to define the value or declare it in a const byte?

This is like reading a pin that your code set to see if you set the pin.

the strings are not part of the code, this is just an example. They are being created in another part of the code and are variable.

i don't think (sizeof(myString) / sizeof(myString[0])) would work because sizeof(myString) returns 8

dschmitty90: the strings are not part of the code, this is just an example. They are being created in another part of the code and are variable.

i don't think (sizeof(myString) / sizeof(myString[0])) would work because sizeof(myString) returns 8

LOL, what Arduino are you planning to run dynamic String arrays on?

and if sizeof(myString[0]) is two, that means 4 elements.

You will either need a sentinal or a global with the number of strings.

No, sizeof() returns the number of bytes allocated to the object. For your calculation to tell how many elements there are, you need to use:

number  = sizeof(myString) / sizeof(char *);

Each element of the array is a pointer, not a string. Therefore the first sizeof() expression returns the total number of bytes allocates for all of the pointers (e.g., 8 bytes). To get the element count, you need to divide by the size of one element, which is the size of a single pointer (e.g., 2 bytes);

Each element of the array is a pointer, not a string. Therefore the first sizeof() expression returns the total number of bytes allocates for all of the pointers (e.g., 8 bytes). To get the element count, you need to divide by the size of one element, which is the size of a single pointer (e.g., 2 bytes);

this makes perfect sense, I thought it was returning 8 because "string 1" is 8 characters not counting the null terminator, but it's because there are 4 pointers 2 bytes each, what an unfortunate coincidence. Thank you I think this solves my issue.

p.s. you know you write a lot of code when you type using a semicolon as a period.

p.s. you know you write a lot of code when you type using a semicolon as a period.

Opps...

because sizeof(myString) returns 8

sizeof(myString) returns 8, because myString contains 4 pointers, and each pointer is two bytes.

On the arduino, a pointer is always 2 bytes.

Therefore, if you want to know how many strings are in the array, sizeof( myString ) / 2 will work.

But actually, defining things the way you have, the array will alway have 4 elements, it won’t change. So this question is somewhat pointless.

If you want to have some kind of array of strings which is dynamically variable length, you’d have to approach the whole problem a slightly different way.

@michinyon. Writing the code as:

sizeof( myString ) / 2

is not good style. First, it uses a magic number, 2. Someone reading the code might wonder why the 2 is there. Second, it's not portable. Take the expression to a 32 bit processor it fails. Writing the expression as:

sizeof(myString) / sizeof(char *)

is preferred because: 1) it doesn't use a magic number, 2) it's self-documenting as to the data type being used for the second operand, and 3) it's portable across platforms with different pointer sizes.

I could point out that if String arrays are to be created dynamically that counters can too but I’m waiting to see if when the other shoe drops the whole house doesn’t come down soon after. But maybe it won’t. Maybe an Arduino with lots of RAM will run it or only short strings and very limited arrays will be used.

I’d go with C strings and reuse of buffer space, see how much can stay in flash and think SD card.

@GoForSmoke: For the Arduino platform, I don't know why one would risk using the String class in lieu of char arrays, other than for convenience