How to maintain original data using strchr

Hi there, I saw this post on how to recover the data using strchr instead of strtok... but I can't get the what are pointers for.. can anyone guide..?

can you clarify your question?

here is the doc for the function: strchr - C++ Reference

if you don't understand pointers, Pointers - C++ Tutorials

There is one answer from the post, saying can be achieved by using two pointers and strchr..
so I am trying to figure out how does it work.. but the example in the post was quite confusing and complicated... That's why I am wondering if there is any easier way to do that, because I totally have no clue on the strchr and pointers..Thanks for sharing the docs.. but I prefer some simple example too

I think that's what @sterretje meant (he also gave a similar example)

char array[] = "1,76,2,0,19:31,- 6.0,E.-dorf Krkhaus,24,107,1,07601";

void setup() {
  Serial.print("\nParsing: "); Serial.println(array);
  char *stratPtr = array, *commaPtr = nullptr;
  do {
    commaPtr = strchr(stratPtr, ',');
    if (commaPtr) {
      *commaPtr = '\0'; // temporary terminate the cString there
      Serial.println(stratPtr); // so we can print the token
      *commaPtr = ','; // restore the array as it was
      stratPtr = commaPtr + 1;
    } else Serial.println(stratPtr); // last text
  } while (commaPtr != nullptr);

void loop() {}

monitor will show

Parsing: 1,76,2,0,19:31,- 6.0,E.-dorf Krkhaus,24,107,1,07601
- 6.0
E.-dorf Krkhaus

Why use strchr? If you want to parse the data without destroying the original string like strtok would, just copy the string and parse the copy using strtok.

sure - but parsing in place saves memory... can help if you are short on memory.

Got it.

because it is in loop function, so it keeps parsing back the same "variable"
for example

strcpy(tempChars, buf);

strtokIndx = strtok (tempChars",");

strtokIndx = strtok (NULL,",");

it works fine for the first run, but not working anymore for the remaining run, because what left in tempChars is only messagesPC, not more for id1 and so on...

I see, but I have more than 1 delimiters..

So you just wasted my time writing an example related to that post with 2 pointers...

each case is different. seems we have discussed that before..
Post a real question, with context and sample code of what you tried.
Don't post indirect questions.

Please read How to get the best out of this forum and post accordingly (including code tags and necessary documentation for your ask).

char *strchr(const char *str, int c) searches the string str and returns a pointer to the first character c. Using that pointer you can calculate out how many characters there are before the first c. That is your first 'token'. If the character isn't found, strchr() returns NULL and your 'token' is the entire remaining string.

Once you have found the first c you can repeat the operation to find the next token.

hmmm even the post I shared is asking for general question,, I dont think that is a need for tat... the concern is like I mentioned before..

do you mind to further explain with 3 delimiters..?


GO to your point. What is YOUR question, YOUR use case in details. read #10 and follow the guidelines please

Alright.. I should give an example, as below:

strcpy(tempChars, buf);

strtokIndx = strtok (tempChars",");

strtokIndx = strtok (NULL,",");

strtokIndx = strtok (NULL,",");

strtokIndx = strtok (NULL,"/");

strtokIndx = strtok (NULL,"|");
value1 = atoi(strtokIndx);   // it is integer

Same as with 1 delimiter. Call strchr() for each of the delimiters and pick the shortest non-NULL token.

I guess it depends If another delimiter is acceptable as part of the input when you are looking for a specific delimiter.
say you have
"3,1415 | 0,1234 | fred, Julie ; 0x22"
the first two tokens require the pipe, then the comma, then the semi colon... (we use the comma in French instead of the dot for decimal point)

➜ we need to know @stephanie9's exact use case. In her example we don't even see the string to parse and she posted snippets again...

Third time asking for full documentation, I'm out of this discussion.

yup,, similar like this.

sure. but still no code, no requirements, no details.
As I said, I can spend my "help time" better elsewhere since you don't want to post your specifics and a demonstration code not snippets. (Snippets R Us!)

What extent of code you expected..? I thought this shows 3 delimiters alry..
addition: maybe the most I missed out the structure of the data only