1
Hello,
I am using Strtok to split a string into words. My idea was to follow the word REMOVE
deleted the next word from the text.
void algorithm(node **root, char *line){
char *pch = strtok(line, " ");
while(pch != NULL){
if(strcmp(pch, "REMOVE")==0){
pch = strtok(NULL, " ");
if(pch == NULL){
printf("error?");
}
removeNode(&(*root),pch);
}else{
insert(&(*root), pch);
}
pch = strtok(NULL, " ");
}
}
Why is it after pch = strtok(NULL, " ");
always have pch = NULL
?
Note: I already tested with large files and a REMOVE in the middle and the pch
after this operation always gives NULL
, if the operation is at the end of the variable cycle pch
no longer stays NULL
.
In this case you should not use strncmp(pch, "REMOVE", 6) and not a simple strcmp?
– user4552
The problem is not in the comparison, it is in the pch call = Strtok(NULL, ""). I will explain when I read a "REMOVE", go to the following word with the pch = Strtok(NULL, ") command and have it removed. The problem is here when I try to access the next word the value returned to pch is NULL.
– urb
But you’re not just comparing the 6 characters of the string pointed by pch to "REMOVE", you’re comparing the entire string that starts in pch to "REMOVE". It will only be the same if "REMOVE" is the end of the string.
– user4552
I did the test with strncmp(pch, "REMOVE", 6) and the error remains the same, because the problem is not in the comparison but in the assignment of pch that always gives null.
– urb
Sorry, I got confused with the commands. You are right to use strcmp.
– user4552
Checks whether the function
removeNode()
alters thepch
?– pmg
&(*apontador)
is the same asapontador
:)– pmg