r/C_Programming • u/UnViandanteSperduto • Oct 07 '26
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).
10
Upvotes
19
u/EpochVanquisher Oct 07 '26
strtok modifies the string, you cannot pass in a string constant. You can split the string manually instead and use start/len or start/end, or you can copy the string to writable memory (e.g. malloc, strdup).
17
u/TheThiefMaster Oct 07 '26 edited Oct 07 '26
The problem isn't usually
char*but string literals ("things in quotes"). You can't modify a literal, you have to copy it first. It can be assigned to a writeablechar*for historical reasons but still isn't actually writeable.Try
char* my_str = strdup("blah blah");strdupis a standard function (C23 or POSIX) that copies a given string into new writeable memory. You should free it withfree(my_str);when you're done with it.Also, showing us the exact code would help, so we don't have to guess what you've written
2
u/garnet420 Oct 07 '26
C doesn't lock anything in read only mode.
char*is not read only.Are you talking about
const char*? Are you trying to use strtok with a string constant?Also strtok is not thread safe, so, don't use it. No reason to build bad habits.
2
u/SmokeMuch7356 Oct 07 '26
String literals are supposed to be immutable; attempting to modify the contents of a string literal results in undefined behavior.
strtokattempts to modify the source string by replacing delimiters with0. Therefore, calls likeor
are bad juju.
The first argument has to point to a writable buffer:
or
2
u/pfp-disciple Oct 07 '26
I'm not certain what you mean, but it looks like you might have a fundamental misunderstanding.
char *only means that it points to achar, with no guarantees regarding read-only. There are also no guarantees that it actually points to a nil-terminated string.A string literal, e.g.
"Hello", is a constant. In most cases, it will be stored in read only memory and will have a segmentation fault if an attempt is made to write to them. Annoyingly,strtokmodifiers the string.A
char*itself can be assigned, but if it points to a string literal then trying to modify the string (e.g.s[1]='x') will fault. As others have said, if you want to usestrtokto parse a string literal then you need to copy its contents to a modifiable part of memory. Look upstrdup,malloc, and thestrcpyfamily of functions for approaches.2
u/musbur Oct 08 '26
Use valgrind. It shows you exactly what went wrong where.
1
u/Interesting_Buy_3969 Oct 07 '26
Make sure the strings you pass to
strtokare mutable. For example, if your string is declared a global variable of type "pointer tochar", and it's declared like this:then declare it as a non-
constarray ofchars:This approach lets you use static memory, with no need for runtime allocations via
strdup, and still be able to modify the string. Under the hood, though, the only difference is that themutable_stris placed in.datarather than.rodataexecutable binary section.1
u/TheCRatTime Oct 07 '26
strtok() modifies string, for example:
char* test = strtok("test string", " "), "test string" will be equals "test\0string", if you callprintf("%s\n", test)output will betest(\0- end of string). You can call again:test = strtok(NULL, " ")and test will be equals NULL, if you call again may be Segmentation fault.1
u/Total-Box-5169 Oct 07 '26
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
1
u/nasccped Oct 07 '26
Hi, buddy. Not sure if I got your question, but I guess that when compiling C's source code, it put all string literals into the
.rodatasection, which turns the data readable but also imutable. I'd never usedstrtokfunction and I don't know exactly how it works, but if it really requires a mutable piece of data, the expected behavior is to get a segfault. You can easily avoid this kind of behavior by using builtin functions, such asstrcpy,malloc, whatever.Or even better, you can define at compile time a buffer using array syntax (
char buffer[1024]).The last one is great since it doesn't require mem. free. A not so great info is that since you're handling the buffer mem at compile time, dealing with unknown size inputs turns into a headache.