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
View all comments
Show parent comment
2
u/Beginning-Junket8979 Oct 07 '26
The language doesn't, but the linker/loader/target hardware might.
const int x=256;is giving the compiler + linker permission to create a.rodatasection containing the value0x00000100that on something like a microcontroller might literally be stored in "read-only" flash memory, or on Linux/Windows/etc might live in a virtual memory page with hardware flags set to mark it read-only memory (throwing a fault/interrupt if the application tries to write it).All modern ARM and x86 CPUs supporting virtual mem support marking memory read-only, read/write, and executable or not as sort of bare minimum needed to support basic user permissions / security and your compiler toolchain and OS is absolutely "smart" enough to utilize this.
I say permission because the language standard doesn't make any promises this happens (to support hardware where it's impossible) and the whole thing might even be compile time optimized out being stored at all.
Sure, but strtok_r is fine 🤷♂️.