C: STRTOK exception [duplicate]
Asked Answered
L

3

6

for some reason i get an exception at the first use of strtok() what i am trying to accomplish is a function that simply checks if a substring repeats itself inside a string. but so far i havent gotten strtok to work

int CheckDoubleInput(char* input){
char* word = NULL;
char cutBy[] = ",_";

word = strtok(input, cutBy);  <--- **error line**

/* walk through other tokens */
while (word != NULL)
{
    printf(" %s\n", word);
    word = strtok(NULL, cutBy);
}
return 1;
}

and the main calling the function:

CheckDoubleInput("asdlakm,_asdasd,_sdasd,asdas_sas");

screenshot of the error im getting

Lava answered 28/12, 2013 at 9:42 Comment(12)
input should point to a modifiable array, don't call your function as CheckDoubleInput("Hello,_word");Deluge
Read strtok causing segfault but not when step through codeDeluge
ok also tried: char* string = "asdasd,_asdasd"; CheckDoubleInput(string); still doesnt workLava
Try as char string[] = "asdlakm,_asdasd,_sdasd,asdas_sas"; Read the answer I linked with your question.Deluge
Flelix You should also learn Difference between char *str` and char str[] and how both stores in memory?`Deluge
ok, so how could i use strtok() on a string im getting as an argument in a function?Lava
Felix:See do like char string[] = "asdlakm,_asdasd,_sdasd,asdas_sas"; CheckDoubleInput(string);" Read the answer I have linked exactly answer your question.Deluge
Grijesh Chauhan, thanks, it works with [], but still...i need it to get char* as an argument. :\Lava
Fleix You don't need to change function arguments type. The problem was you were passing a constant string literal that can't be modified. So I linked the another question to learn differences between char* and char[]Deluge
thanks alot! i will read it nowLava
You can start the process from the copy inside the function, to change the check of a type that does not break the string.Cavour
Please copy the text from the screenshot into the body of the question. You can use "Ctrl-C" to copy the text from most dialog boxes in Windows.Champerty
B
2

CheckDoubleInput() is ok. Look at this. Hope you will understand

int main(){
    char a[100] = "asdlakm,_asdasd,_sdasd,asdas_sas";
    // This will lead to segmentation fault.
    CheckDoubleInput("asdlakm,_asdasd,_sdasd,asdas_sas");
    // This works ok.
    CheckDoubleInput(a);
    return 0;
}
Barny answered 28/12, 2013 at 10:49 Comment(3)
No it is confusing // This will lead to segmentation fault Why ?? I think explain it..Deluge
@Grijesh Chauhan: because it's a pointer to a constant. strtok() modifies first argument.Barny
Oh so you means your comment is for second line not for second line ... got it.Deluge
R
1

The strtok function modify its first input (the parsed string), so you can't pass a pointer to a constant string. In your code, you are passing a pointer to a string literal of char[N] type (ie. a compilation constant string) and hence trying to modify a constant string literal which is undefined behaviour. You'll have to copy the string in a temporary buffer before.

char* copy = strdup("asdlakm,_asdasd,_sdasd,asdas_sas");
int result = CheckDoubleInput(copy);
free(copy);

Here is what the man page for strtok says:

Bugs

Be cautious when using these functions. If you do use them, note that:

  • These functions modify their first argument.
  • These functions cannot be used on constant strings.
  • The identity of the delimiting byte is lost.
  • The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.
Reference answered 28/12, 2013 at 10:6 Comment(2)
thank, but i cant initialize char* copy = strcpy("asdlakm,_asdasd,_sdasd,asdas_sas"); like that, i need it to get the *char sent as argumentLava
@FelixKreuk Actually it should be strdup() updated answer. read againDeluge
H
0

Either input is somehow bad. Try printing it before calling strtok OR you're using strtok on multiple threads with GCC compiler. Some compilers have a thread safe version called 'strtok_r'. Visual Studio have fixed the original function to be thread safe.

Your modified answer shows that you're passing a string literal which is read only.

Habitation answered 28/12, 2013 at 9:49 Comment(2)
you can see that the value of input is ok in the picture i attachedLava
It's not OK, passing a string literal is dangerous. pass copy of it as strtok will modify it. The memory where the string literal are stored is read only.Habitation

© 2022 - 2024 — McMap. All rights reserved.