You're not using strtok
itself wrong, but you are using C string literals wrong.
void mkdir(NodePtr *root, char pathname[64])
This function prototype is equivalent to void mkdir(NodePtr *root, char *pathname)
and should be written that way. I mention this because it's important to understand that you are passing strings into mkdir
by reference.
mkdir(&root, pathname);
mkdir(&root, "/abc/fa");
On the first call, the pathname
argument is set to point to the pathname
variable in main
, which is in writable memory, so everything works.
On the second call, the pathname
variable is set to point to the string literal "/abc/fa"
, which is in read-only memory, so you crash. You would get the same crash if you did anything in mkdir
that attempted to modify the array pointed to by pathname
, via a library function or otherwise.
The simplest available cure is to write
char pathname2[64] = "/abc/fa";
in main
, and then pass that to mkdir
; this causes the compiler to generate a copy from the string literal to another writable character array. More sophisticated approaches are possible, and even advisable, but I'd have to know a lot more about what your larger goals are.