The first if condition leads to a segmentation fault. I can't really comprehend why, since I use similar if clauses with relational operators elsewhere. Thanks for your help in advance.
int foo(char *str1,char **str2, char **str3)
{
char *token1;
char *token2;
char *token = strtok(str1, "\"");
int spaces = strcmp(token," ");
int parenthesis = strcmp("{",token);
if((token == NULL) || ((spaces != 0) && (parenthesis != 0)))
{
printf("ERR.\n");
return 0;
}
token = strtok(NULL, "\"");
if(token == NULL)
{
printf("2ERR\n");
return 0;
}
token1= strtok(NULL, "\"");
if(token1 == NULL || strcmp(token1," -> ") != 0)
{
printf("3ERR\n");
return 0;
}
token2 = strtok(NULL, "\"");
return 1;
}
The
strtok
will crash if you don't usechar[]
. You can get around it usingstrcpy
.I'm not sure what you're trying to do, but your code will run without crashing after the small change.