Problem with C program never returning from strtok() function
I am working on a university assignment and I've been wracking my head around a weird problem where my program calls strtok
and never returns.
My code looks like:
int loadMenuDataIn(GJCType* menu, char *data)
{
char *lineTokenPtr;
int i;
lineTokenPtr = strtok(data, "\n");
while (lineTokenPtr !=开发者_如何学运维 NULL) {
/* ... */
}
}
I've looked up a bunch of sites on the web, but I cant see anything wrong with the way that I am using strtok
and I cant determine why it would my code would get stuck on the line lineTokenPtr = strtok(data, "\n");
Can anyone help me shed some light on this?
(Using OSX and Xcode if it makes any difference)
have you checked the contents of the argument? is it \0 terminated?
the argument that you pass, is it writeable memory? strtok writes to the buffer that it gets as first argument when it tokenizes the string.
IOW if you write
char* mystring = "hello\n";
strtok(mystring,"\n"); // you get problems
The function strtok()
replaces the actual token delimiting symbols in the character string with null (i.e., \0
) chars, and returns a pointer to the start of the token in the string. So after repeated calls to strtok()
with a newline delimiting symbol, a string buffer that looked like
"The fox\nran over\nthe hill\n"
in memory will be literally modified in-place and turned into
"The fox\0ran over\0the hill\0"
with char
pointers returned from strtok()
that point to the strings the fox\0
, ran over\0
, and the hill\0
. No new memory is allocated ... the original string memory is modified in-place, which means it's important not to pass a string literal that is of type const char*
.
精彩评论