c - About some settings of strtok() -


this warning getting:

passing argument 1 of ‘strtok’ discards ‘const’ qualifier pointer target  type [enabled default] 

i wanted disable default operation can me this?

thank you!

strtok works in-place: needs tokenize string passed it.

of course, force non-const cast violate contract. if caller expects re-use passed string after operation? it's no-go.

so if have constant string, have make copy before using it, instance using strdup

char *copy = strdup(my_const_char); toks = strtok(copy," ",null); ... 

in end, have tokens in separate pointers, memory allocated , held copy. once don't need tokens anymore, freeing copy need clean up.

note generic answer const qualifier question is: passing argument 1 discards qualifiers pointer target type


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -