开发者

Different results using %c and loop vs. %s in printf with null terminated string

I have a variable 'jmp_code' that is declared as a char *. When I run th开发者_StackOverflowe following commands

printf("char by char, the code is '%c%c%c%c'\n", *jmp_code, *(jmp_code+1), *(jmp_code+2),*(jmp_code+3));
printf("printing the string, the code is '%s'\n", jmp_code);

I get the following results

char by char, the code is '0,0,0, ,'
printing the string, the code is 'ö\├w≡F┴w'

I am using codeblocks. Here is the sample code I am playing with.

#include <stdio.h>
#include <string.h>

char * some_func(char * code);

char * some_func(char * code) {

    char char_array[4];

    strcpy(char_array, "000");

    code = char_array;

    return code;

}
int main ( void ) {

    char * jmp_code = NULL;

    jmp_code = some_func(jmp_code);

    printf("char by char, the code is '%c,%c,%c,%c,'\n", *jmp_code, *(jmp_code+1), *(jmp_code+2),*(jmp_code+3));
    printf("printing the string, the code is '%s'\n", jmp_code);

    return 0;

}

I am quite confused by this. Any help would be appreciated.

Thanks


Some quick observations:

char * some_func(char * code) {
    char char_array[4];
    strcpy(char_array, "000");
    code = char_array;
    return code;
}

You can't assign strings using = in C. That messes things up - you're assigning code the pointer of your locally allocated char_array to code, but you're not copying the contents of the memory. Also note that since char_array is allocated on the stack (usually), you'll find it disappears when you return from that function. You could work around that with the static keyword, but I don't think that's the nicest of solutions here. You should use something along the lines of (big warning this example is not massively secure, you do need to check string lengths, but for the sake of brevity):

void some_func(char * code) {
    strcpy(code, "000");
    return;
}

(Refer to this (and this) for secure string handling advice).

And call it via some_func(jmp_code) in main. If you're not sure what this does, read up on pointers.

Second problem.

char * jmp_code = NULL;

Currently, you've declared space enough for a pointer to a char type. If you want to use my suggestion above, you'll need either to use malloc() and free() or else declare char jmp_code[4] instead, such that the space is allocated.

What do I think's happening? Well, on my system, I'm getting:

and the code is '0,0,0,,' and the code is ''

But I think it's chance that jmp_code points to the zeros on the stack provided by your some_func function. I think on your system that data has been overwritten.

Instead you're reading information that your terminal interprets as said character. Have a read of character encoding. I particularly recommend starting with The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)


You're returning a reference to a temporary array. char_array goes away when some_func() retuns, but you keep using the address of it. You need to use malloc() to allocate an array and then free() it after you use it.


You're printing from an invalid pointer. char_array is on the stack of some_func() function.
The function returns the pointer of something that is on the stack and will be no more after the function returns!
The first printf finds the stack still unchanged, the second, maybe, found it filled with... garbage!


It might be interesting to see:

const char *pos = jmp_code;
while (*pos)
    printf("%d ", *pos++);


I think char type can not use non-ascii char codes. Meaning your string contains UTF-8 or like symbols which code could be in (0, over9000) range, while char codes could be in (0, 255) range.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜