Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: strtok() and the mistery of the same address strings with different lengths... Date: Fri, 13 Jan 2017 16:49:26 -0800 Organization: None to speak of Lines: 32 Message-ID: References: <94292e94-bcb9-40ea-8b8a-2a8563eeb33e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="ba0db46df61d502ee0e6afe10bb5a782"; logging-data="32712"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX196ro8j4YZ2CDsRolPwM4Ha" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:hSo8tLisDS5wzjqztr/KDO0yBZk= sha1:IToRAUHNjEXMkIKpRdsZKWcL6QY= Xref: csiph.com comp.lang.c:101699 "Fernando D. Bozzo" writes: > Ok, this is driving me crazy since 2 hours now. [...] > ...How is possible that the function strtok() could return different > strings --each with it's own \0 termination-- in different string > variables without dinamic memory? [...] strtok() modifies its input string in place. The first time you call it with a non-NULL second argument, it remembers the location of the string you're tokenizing. (This is a real problem if you want to tokenize two different strings concurrently.) If you start with a string (showing the terminating '\0' explicitly) "abc def ghi\0" and use strtok() to split it on ' ' delimiters, then after you're done looping over it, the same memory will contain: "abc\0def\0ghi\0" A pointer to the 'a', to the 'd', or to the 'g' will point to a valid null-terminated string with a length of 3 characters. The original 11-character string "abc def ghi" will have been clobbered. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"