What causes the error
Doing tokens = strtok(NULL ," ");
twice at the beginning of the while loop looks pretty wrong:
tokens = strtok (statement," ");
while(tokens!= NULL)
{
tokens = strtok(NULL ," "); // tokens might become NULL here!
IMHO it's better readable and more safe to put this in a for
loop:
for(tokens = strtok (statement," "); tokens != NULL; strtok (NULL ," "))
{
// ...
}
Note that tokens
may become NULL
after the call inside the while loop, and is passed unchecked to isDouble()
in the else
part. This might quite probably be your problem.
and what can I do to prevent the error?
Always check pointers for non NULL values before dereferencing them.