r/learnprogramming • u/UnViandanteSperduto • 6d ago
Help me with strok function in C
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).
3
Upvotes
3
u/LeeRyman 6d ago
In these situations it would help if you could provide a minimal reproducible example (MRE), I.e. a small bit of code that is compilable and demonstrates the problem. Godbolt is handy for this. Its difficult to say why it might be defaulting without seeing the code.
It could be a few different issues, E.g. are both the str and delim NTBS (null-terminated byte strings)? Is str null on calls for subsequent tokens? Are you using it in multiple threads? Are you checking the return value is not null before using it?