I am trying to understand how to pass arrays of character strings to a function. I know I can probably do it easier with String objects, but the project I am working on is probobly going th be pretty big so memory may be an issue..
So, if I define some arrays like this ...
char *array1[] = {“one”,”two”,”three”,”\0”}
char *array2[] = {“alpha”,”Baker”,”charlie”,”delta”,”\0”}
I would like to pass them to a function like...
doSomethingWith(array2);
void doSomethingWith(char *ar) {
Serial.println( ar[1]). // would be “two” if passed array1 or “Baker” if passed array2
}
I know the above is not a functional syntax, but not sure what is. I think there is also a problem with passing an array of indetermanent length (hence the “\0” to find the length.) In most of the languages I’ve used there was a way to handle this, but I am not familiar enough with c++ to figure it out. Any suggestions? I’ve googled the crap out of this, but still haven’t found a relivent solution that I could implement.
My guess there might be some cludge that would work. Still looking.
Two things I don’t do well: brain surgery and explaining my problem)
If you want to pass variable length arrays, then either you need to pass the length, or the array needs a terminator. For arrays of pointers to char (which is what an array of C strings is), a good choice is to use NULL as a terminator rather than using a zero-length string. In conditional expressions, a NULL is treated as a logical false, and this creates compact code that other C programmer can understand.
The type of these paramters is [nobbc]char **x[/nobbc], or equivalently [nobbc]char *x[][/nobbc]
char *array1[] = {"one","two","three",NULL};
char *array2[] = {"alpha","Baker","charlie","delta",NULL};
void foo(char **s) {
while(*s) {
Serial.print(*s);
s++;
}
}
void bar() {
foo(array1);
foo(array2);
}
Although the name 'array1' is a constant, the name 's' in the function 'foo' is not.
To do the equivalent the way you are doing it - using a pointer to an empty string as the terminator, you would:
char *array1[] = {"one","two","three",""};
char *array2[] = {"alpha","Baker","charlie","delta",""};
void foo(char **s) {
while(**s) { // DETECT IF THE INDEXED STRING IS OF ZERO LENGTH
Serial.print(*s);
s++;
}
}
void bar() {
foo(array1);
foo(array2);
}
Using "\0" is unnecessary - the double quotes already get a nul-terminated string. Using "\0" crates an array of char of length 2.
Throwing in some 'const' declarations will stop the compiler warnings:
#include "Arduino.h"
void doSomethingWith(char const **);
void setup() {
Serial.begin(115200);
delay(2000);
char const *array2[] = { "alpha", "Baker", "charlie", "delta", nullptr };
doSomethingWith(array2);
}
void loop() {
}
void doSomethingWith(char const **ar) {
while (**ar) {
Serial.println(*ar);
ar++;
}
}
while(**s) { // DETECT IF THE INDEXED STRING IS OF ZERO LENGTH
That is NOT what that code does.
Ok, you'all got me much closer. Using the actual array's I am trying to use, here is what I came up with...
#include "Arduino.h"
char const *menu1[] = {"Options","Start","Movement","Shutter",NULL};
char const *menu1_1[] = {"Step Size","Settle Time","Pause Time","Frames","Distance","Exit",NULL};
void displayMenu(char const **);
void setup() {
Serial.begin (9600);
}
void loop(){
displayMenu(char const **menu1); // <---- error highlights this line.
}
//for now, I simply want to print the number of elements.
void displayMenu(char const **a) {
int ln = 0
while (**a) {
a++;
ln++;
Serial.println(ln);
}
this results in the following error: "expected primary-expression before 'char'
highlighted line is displayMenu(char const **menu1);
not really sure what its telling me.
displayMenu(char const **menu1); That almost looks like a function prototype.
TolpuddleSartre:
That almost looks like a function prototype.
hmmmm... well, the intention was to call the function. I have a prototype up above the line that is throwing the error. Maybe you could give me a clue of what I am doing wrong. Me and C++ are kind of new acquaintances. char string arrays and function prototypes are things that are relatively new to me.
See my Reply #2. Function call should just be:
displayMenu(menu1);
gfvalvo:
See my Reply #2. Function call should just be:
displayMenu(menu1);
Opps..your right. I missed that. Works exactly as I need it to now. Thank you all very much. Once again, I've learned a lot.