Search code examples
ccstringstrcpy

explanation of what my code is doing (C)


char *extractSubstring(char *str)
{
    char temp[256];
    char *subString; // the "result"

    printf("%s\n", str); //prints #include "hello.txt"
    strcpy(temp, str); //copies string before tokenizing    

    subString = strtok(str,"\""); // find the first double quote
    subString = strtok(NULL,"\"");   // find the second double quote

    printf("%s\n", subString); //prints hello.txt

    strcpy(str, temp); //<---- the problem

    printf("%s", subString); //prints hello.txt"
    return subString;
}

After I strcpy, why does it add a quotation? When I comment out the 2nd strcpy line, the program works. The printfs will be deleted out of my program. I was just using it to show what was happening with my program.

Can someone please explain to me what is going on? Thank you.


Solution

  • It is important to realize that strtok() modifies the source string in-place, and returns pointers into it.

    Thus, the two calls to strtok() turn str into

    #include \0hello.txt\0
               ^ subString points here
    

    (For simplicity, I don't show the final terminating \0).

    Now, the second ("problematic") strcpy() changes str back to:

    #include "hello.txt"
              ^ subString still points here
    

    This is what makes the " reappear in subString.

    One way to fix it is by tokenizing a copy and keeping the original intact. Just make sure that your function doesn't return a pointer to an automatic variable (that would go out of scope the moment the function returns).