K
Keith Thompson
Paul Connolly said:The main problem is that it modifies the string it acts on. In the
particular case, you're getting it from getenv(). The standard says
about that function:
Returns
[#4] The getenv function returns a pointer to a string
associated with the matched list member. The string pointed
to shall not be modified by the program, but may be
overwritten by a subsequent call to the getenv function. If
the specified name cannot be found, a null pointer is
returned.
So to use strtok() in a defined manner, you would have to make a copy
of the string first, which might be an unnecessary step. Without more
details on what you mean by "parse" and what you will do with the
resultant parsed information, it's hard to say what will be most useful.
why doesn't getenv return a const char *?
Probably for historical reasons. My guess is that the getenv()
function predates the introduction of "const" to the C language.
Existing code that did something like this:
char *foo = getenv("FOO");
would have been broken by such a change. It's similar to the fact
that string literals aren't const; ideally they should be, but it
would have broken existing code.