Function to get the length of an array of Strings

Hi all,

I’d basically need to develop a generic function to get the length of an array of string that is passed as parameter to a function.

I included in the following code 2 functions:

the first one returns the length of a specific array and works correctly,
the second one should return the length of the array that is passed as parameter.

I didn’t code the generic function, since here is the problem I’m facing (how to implement it), but I included some code to display what I can obtain.

Maybe checking the values untill NULL is found? I've done this but did not work.

(Note. Please do not focus on some aspects as the use of Serial.println((String)…. Instead of cout, etc, it’s not part of the problem)

I really appreciate any help with this code... Thanks!


#include "Arduino.h"

int getArrayLen();
int getArrayLen(String array[]);

String cmds[] = { "c1","c22", "c333", "c4444", "c55555"};

void setup() {
Serial.begin(9600);
Serial.println((String)"getArrayLen(): "+getArrayLen()); // Correctly returns 5
//
Serial.println(getArrayLen(cmds));

}

// The loop function is called in an endless loop
void loop() {
//Add your repeated code here
}

// Generic function
int getArrayLen() {
return (int)sizeof(cmds)/sizeof(cmds[0]);
}

// Specific function
int getArrayLen(String array[]) {
int arbitrary = 10;
for (int i = 0; i < arbitrary; ++i) {
Serial.println((String)"array: "+ *array);
array = array +1;
}
return 0;
}


OUTPUT

getArrayLen(): 5
array: c1
array: c22
array: c333
array: c4444
array: c55555
array: 555

array: ºö¿ûìã
array: ¶Ó2U_›nÎ<�_rX_¤
H_¿Gö¤Ò-N–ïp¥Wî_—{©ÿÝè³9\Ó�û}¼¿ƒëw<
0

#include "Arduino.h"

int getArrayLen();
int getArrayLen(String array[]);


String cmds[] = { "c1","c22", "c333", "c4444", "c55555"};


void setup() {
   Serial.begin(9600);
   Serial.println((String)"getArrayLen(): "+getArrayLen());   // Correctly returns 5
   //
   Serial.println(getArrayLen(cmds));

}

// The loop function is called in an endless loop
void loop() {
   //Add your repeated code here
}

// Generic function
int getArrayLen() {
   return (int)sizeof(cmds)/sizeof(cmds[0]);
}


// Specific function
int getArrayLen(String array[]) {
   int arbitrary = 10;
   for (int i = 0; i < arbitrary; ++i) {
      Serial.println((String)"array: "+ *array);
      array = array +1;
   }
   return 0;
}

Why use "arbitrary = 10;" when you can use "arbitrary = getArrayLen();"?