开发者

printf modifying a string

开发者 https://www.devze.com 2022-12-13 10:08 出处:网络
Using printf to print \"\\4unix\\5lancs\\2ac\\2uk\\0\" I find, instead of a print in the form of ♦unix♣lancs☻ac☻uk, I get garbage (♫ ,►E¦§Qh ↕).

Using printf to print "\4unix\5lancs\2ac\2uk\0" I find, instead of a print in the form of ♦unix♣lancs☻ac☻uk, I get garbage (♫ ,►E¦§Qh ↕).

I cannot find an explanation for this; I use the following method to tokenise a string:

/**
 * Encode the passed string into a string as defined in the RFC.
 */
char * encodeString(char *string) {
    char stringCopy[128];
    char encodedString[128] = "";
    char *token;

    /* We copy the passed string as strtok mutates its argument. */
    strcpy(stringCopy, string);

    /* We tokenise the string on periods. */
    token = strtok(stringCopy, ".");

    while (token != NULL) {
        char encodedToken[128] = "";

        /* Encode the token. */
        encodedToken[0] = (char) strlen(token);
        strcat(encodedToken, token);

        /* Add the encodedString token to the encodedString string. */
        strcat(encodedString, encodedToken);

        /* Prepare for the next iteration. */
 开发者_如何学运维       token = strtok(NULL, ".");
    }

    /* A null character is appended already to the encoded string. */

    return encodedString;
}

And the following code in my driver to print the result when tokenising "unix.lancs.ac.uk":

int main(int argc, char *argv[]) {
    char *foo = "unix.lancs.ac.uk";
    char *bar = encodeString(foo);

    printf("%s\n", bar);

    return 0;
}

If I add a printf to print encodedString at the end of the encodeString method, I don't get garbage printed out (rather, ♦unix♣lancs☻ac☻uk twice).

(Upon debugging I notice the actual memory contents is changed.)

Can anyone explain this phenomenon to me?


You are returning a pointer to the array encodedString, which is local to the encodeString() function and has automatic storage duration.

This memory is no longer valid after that function exits, which is what is causing your problem.

You can fix it by giving encodedString static storage duration:

static char encodedString[128];

encodedString[0] = '\0';

(You can no longer use the initialiser to empty the array, because arrays with static storage duration maintain their values from one invocation of the function to the next.)


When you say:

 return encodedString;

you are returning a local variable, which will have ceased to exist by the time you come to use it. A quick hack would be to make encodedString static.


DO NOT use functions that return strings.

Send the string address as paremeter and change it in the function.

The compiler should display a warning. Don't ignore compiler warnings especially in C.

Your function should be like this:

void encodeString(char *string, char *encodedString)
{
.
.
.
}

See here

0

精彩评论

暂无评论...
验证码 换一张
取 消