I have a problem with strtok(). I have string in this form 2000;ON_0;1000;ON_2; and when I execute this code:

for ((p=strtok(Seq_String,";"));p;(p= strtok(NULL, ";" ),n++)){
	 foundThings[n] = p; 

I get :

It is C code for microcontroller and this is not in interrupt, it is in endless loop.

Pleas help :)


This works for me

int main()
    char Seg_String[] = "2000;ON_0;1000;ON_2";
    char* ptr = strtok(Seg_String,";");
        ptr = strtok(NULL, ";");


thank you for fst answre :) it works for me too, but I think now that my problem is in string receiving from PC


I did that at the begining and it is the correct string,I will try to make a beckup of the string because I think that my ISR is called during the string parsing and it overwrites my original string.

I receive may strin like this:

char* Seq_String;
Single_Char = Get_Char_pc();

2 lines beafor I try to parse my string I print it and it is ok, but when I try to copy it or do strtok it does it wrong but when I type my string like this:

char Seg_String[] = "2000;ON_0;1000;ON_2";

it works.

That is a huge problem. String_received is already a pointer, and you are passing a pointer to a pointer, which is wrong. Also, if Single_Char is what that name implies (defined as char and not char*) then you can't use strcat() with it because strcat() expects a null-terminated array of characters.

Third: what is Seq_String? you don't need it because after strcat() is done String_received will contain the entire new string. All strcat() does is return either String_received or NULL, depending on error or not.

My Single_Char is defined: char* Single_Char;
Do you know how to assemble my Seq_String from more Single_Char s?

maybe something like this, which will leave String_received untouched. This allocates new memory for Seg_string, so don't forget to free() it sometime later after you are done with it.

char* Seg_string = 0;
char* Single_Char = Get_Char_pc();
Seg_string = malloc(strlen(String_received) + strlen(Single_Char) + 1);
strcpy(Seg_string, String_received);
strcat(Seg_string, Single_Char);
This article has been dead for over six months. Start a new discussion instead.