This is the warning I am getting:
passing argument 1 of ‘strtok’ discards ‘const’ qualifier from pointer target
type [enabled by default]
I wanted to disable this default operation can anyone help me with this?
Thank you!
This is the warning I am getting:
passing argument 1 of ‘strtok’ discards ‘const’ qualifier from pointer target
type [enabled by default]
I wanted to disable this default operation can anyone help me with this?
Thank you!
strtok
works in-place: it needs to tokenize the string you passed to it.
Of course, you could force non-const cast but that would violate the contract. What if the caller expects to re-use the passed string after your operation? So it's a no-go.
So if you have some constant string, you have to make a copy before using it, for instance using strdup
char *copy = strdup(my_const_char);
toks = strtok(copy," ",NULL);
...
In the end, you have all your tokens in separate pointers, with memory already allocated and held by copy
. Once you don't need the tokens anymore, free
ing copy
is all that you need to clean it up.
Note that a generic answer to this const qualifier question is: Passing Argument 1 discards qualifiers from pointer target type