[SOLVED]Strtok wont compile correct

Mostly before I include a function in my main sketch I prefere to try it it.
So the same wiht the strtok() function.
But even this simple setup would not compile due to char array errors..
I am not a super code writer so p[leasre advise whats wrong here

// testing strtok() functions
void setup() {
  Serial.begin(9600);// setup communication for output
  // declare char array as received true sSerial.read()
  char strValue[] = "123.6,090.0\0";
  char * strXvalue; // should contain first part of received array
  char * strYvalue; // for the second part
  strXvalue = strtok)strValue, ','_; // get first value
  strYvalue = strtok)null, ','_; // the second one
  Serial.println(strXvalue);
  Serial.println(strYvalue);
}

You had the strtok() syntax wrong

// testing strtok() functions
void setup()
{
  Serial.begin(115200);// setup communication for output
  // declare char array as received true sSerial.read()
  char strValue[] = "123.6,090.0\0";
  char * strXvalue; // should contain first part of received array
  char * strYvalue; // for the second part
  strXvalue = strtok(strValue, ","); // get first value
  strYvalue = strtok(NULL, ','); // the second one
  Serial.println(strXvalue);
  Serial.println(strYvalue);
}

void loop()
{
}

@UKHeliBob the delimiter(s) parameter in the second strtok call should also be a C string.
@gharryh So as UKHeliBob already said, the syntax is wrong. To prevent errors like this in the future, you could look up the required syntax for each function. (e.g. strtok C++ reference)

Another thing:

char strValue[] = "123.6,090.0\0";

The '\0' at the end supposed to be the null termination for the string? You don't need to do that, compiler will do that for you. Just pay attention, if you initialize the char array with explicit size, leave one byte for the null termination.

You are correct. I neglected to change it from the original code

Sorry you are wrong
Complle error:
cannot convert 'char*(char*, const char*)' to 'char*' in assignment

I am not sure what code you are trying to compile so please post it here

Despite the fact that I neglected to change ',' to "," when amending your code, the sketch that I posted compiles

the following results from the code below

123.6
090.0
// testing strtok() functions
void setup()
{
    Serial.begin(115200);// setup communication for output
    // declare char array as received true sSerial.read()
    char strValue[] = "123.6,090.0\0";
    char * strXvalue; // should contain first part of received array
    char * strYvalue; // for the second part
    strXvalue = strtok(strValue, ","); // get first value
    strYvalue = strtok(NULL, ","); // the second one
    Serial.println(strXvalue);
    Serial.println(strYvalue);
}

void loop()
{
}

the 2nd argument is a string, not a char

You even get the same output form my improperly constructed code !

I suspect that @gharryh is still using strtok() wrongly

Something went wrong during copying from the forum, so I had to type the complete sketch from scratch and it works. thanks for the help

all i had to do was change the single quotes to double quotes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.