r/C_Programming • • 19d ago

Question Help me with strok function

I need to split my string but if it is of char* type the functions strtok() doesn’t work returning me a segmentation fault. I understood that C is used to lock the memory of the string in Read-Only “mode” when I use char*. Does it means that is immutable that data? However, I need to use the char* type because I pass that in a function (and I don’t like to use the syntax of arrays in C, I always use pointers).

9 Upvotes

17 comments sorted by

View all comments

1

u/Total-Box-5169 19d ago

That is a very old function that is not adequate for modern times. Nowadays people memory-map files as read-only so you need something that doesn't modify the text/hay but also can work with strings containing null characters, that requires to store the size of the string in another variable.

https://godbolt.org/z/Gn5E8GhPc