|
James Rich wrote: > > Hmm... I didn't even know about the strtok() function. Oh here's why - > from the man page: > > BUGS > Never use these functions. If you do, note that: > > These functions modify their first argument. > > The identity of the delimiting character is lost. > > These functions cannot be used on constant strings. > > The strtok() function uses a static buffer while > parsing, so it's not thread safe. Use strtok_r() if > this matters to you. James, your function has two of those problems. It modifies the "line" argument (completely destroying it, which is much worse than what strtok does), and it can't be used on a constant string. But these two problems with strtok can be handled as easily as the thread-safety one: just copy the source string to a temporary string and parse the temp. The only real problem with strtok is the one about the delimiting character being lost. This problem can also be solved by copying to a temp. Doing this one single copy would be WAY less copying than your function has - your function repeatedly copies the source string to itself (shorter and shorter amounts each time) AND it copies the source string into the token strings. (If that last copy is necessary, you can still do it using strtok).
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.