Hey guys I'm working on a function
which is passed two consecutive strings separated by null character, these strings are directory names that I need later in the function to open a directory. I was thinking I could use,
void *copydirectory(void *arg)
char *strtok(char *str, const char *delim);
since my function needs to be thread safe. Can I use a null character as a delimiter? For example,
char *strtok_r(char *str, const char *delim, char **saveptr);
Additionally, would the names being pointed to be non null terminated after the calls to strtok_r? Any tips on other strategies would help.
char *dir1, *dir2;
char *delim = '\0';
dir1 = strtok_r(arg, delim, &saveptr);
dir2 = strtok_r(NULL,delim, &saveptr);
No, this will not work. Since the null character marks the end of a string, strtok() wouldn't know that you're overloading its meaning.
Since you just have two strings concatenated, it's easy to pull them apart:
char arg = "/foo\0/bar";
char *first, *second;
first = arg;
second = arg + strlen(arg) + 1;
I see. Thanks for your input.