expected unqualified-id before '[' token

Trying to implement the following to split a string

but get the following error on compile

expected unqualified-id before '[' token

I believe the problem method is below.
Can anyone help?

String[] splitCommand(String text, char splitChar) {
      int splitCount = countSplitCharacters(text, splitChar);
      String returnValue[splitCount];
      int index = -1;
      int index2;
 
      for(int i = 0; i < splitCount - 1; i++) {
        index = text.indexOf(splitChar, index + 1);
        index2 = text.indexOf(splitChar, index + 1);
 
        if(index2 < 0) index2 = text.length() - 1;
        returnValue[i] = text.substring(index, index2);
      }
 
      return returnValue;
    }
String[] splitCommand(String text, char splitChar) {

You can't return an array from a C or C++ function.

What Paul says... +1.

What you can do is pass a pointer to the first element of an array but I don't want to think of the overhead that a String array would have to entail if it's possible on an Arduino at all.

I would suggest that the OP learns about C pointers and C strings and avoid C++ Strings like the plague on Arduinos.

Paul nailed your problem and GoForSmoke has sage advice...the String class is from C++ and is usually an H-bomb-to-kill-an-ant approach to text processing. Your code could be replaced with something like:

void setup() {
   char buffer[20];   // I have no idea how big you need this, I just made one up...
   char input[20];
   char *ptr;

   ptr = SplitCommand(input, splitChar, buffer);
   // Whatever you need to do...
}
   
char *splitCommand(char *text, char splitChar, char buffer[]) {
      int splitCount = countSplitCharacters(text, splitChar);
      int index = -1;
      int index2;
 
      for(int i = 0; i < splitCount - 1; i++) {                 // Not sure this is what you really want...
        index = text.indexOf(splitChar, index + 1);
        index2 = text.indexOf(splitChar, index + 1);
 
        if(index2 < 0)
           index2 = text.length() - 1;
        buffer[i] = text.substring(index, index2);
      }
 
      return buffer;
    }

(I didn't check to see if your code is doing what you think it should...only the syntax structure of the code.)

The first line of the function definition:

char *splitCommand(char *text, char splitChar, char buffer[]) {

is broadly built of two parts: 1) the function type specifier, which tells the type of data returned from the function, a char pointer (char *) in this example, and 2) the function signature, which extends from the start of the function name through the closing parenthesis (i.e., splitCommand(char *text, char splitChar, char buffer[]) ) As Paul pointed out, your function type specifier was wrong, because you cannot return an array from a function. You can, however, return a pointer to it which is what I've done here. Not returning an array makes sense if you think about it, because the compiler would have to copy the entire array onto the stack when the function code was done, and then pop it off the stack when it returned. This would unnecessarily take time and memory.

Also note in the signature that char *test could be written as char test[], as I did for buffer. Also, an array name, like buffer is viewed by the compiler as though you wrote it &buffer[0]. That is, the array name is the same as the memory address for the first element of the array.

For some situations the way to split a C string is with strtok(). For others you need 2 buffers and memmove(). And you really should understand pointers first.

I bet that code came from Java.

michinyon:
I bet that code came from Java.

Looked like C# to me (which is, of course, a ripoff of Java).

With the advertising strap line:-
C# - I can't believe its not Java

econjack:
the String class is from C++ and is usually an H-bomb-to-kill-an-ant approach to text processing

They're all similar enough that you'd never know for sure unless you were able to follow the designer's thoughts, but there are quite a few aspects of the Arduino language and Wiring API that I suspect derive from Java rather than C++. I suspect that the designers started off with the aim of making the Arduino language look as similar as they could to the Processing language. This has lead to some highly questionable design decisions such as incorporating serial event handlers into the basic structure of the sketch (but not in a way that could be extended to support any other event handlers), and automagically generating function prototypes (but in a very naive and fragile way), and incorporating classes like String which tries to dumb down text processing - but does it in a way that is not at all sensible in a microcontroller environment. It makes me wish the designers had just bitten the bullet and admitted from the outset that an Arduino is a microcontroller and the Arduino language is C++.