Re: Why occur the mistake in runtime about strtok()?



"hu" <huhu1971@xxxxxxxxx> writes:
I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0.
Program is simple, but mistake is confusing. First, the below code can get
right outcome:"ello world, hello dreams."

#include <stdafx.h>
#include <string.h>
#include <stdio.h>

int main()
{
char *pStr = "Hello world, hello dreams.";
char *p = pStr;
p = strtok(pStr, "H");
if (NULL==p)
printf("null__");
else
printf("%s", p);

return (0);
}

Second, Changing the sentence:
p = strtok(pStr, "H");
to
p = strtok(pStr, "Ho");
It can be compiled, but cannot run!

Third, Changing the sentence:
p = strtok(pStr, "H");
to
p = strtok(pStr, "e");
It can be compiled, but cannot run too!

Where is the wrong?

Three things. First, you're using a non-standard header <stdafx.h>.
Second, you're trying to modify a string literal (that's your real
problem). Third, you're you're not telling us how it fails; "cannot
run" wouldn't have given us much information if the actual problem
didn't happen to be obvious.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages


Loading