我有一个变量'jmp_code',声明为char *。当我运行以下命令不同的结果使用%c和循环与printf中的循环与%s以空值终止的字符串
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);
我得到下面的结果
char by char, the code is '0,0,0, ,'
printing the string, the code is 'ö\├w≡F┴w'
我使用的代码块。这里是我玩的示例代码。
#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;
}
我对此很困惑。任何帮助,将不胜感激。
感谢
我认为这与jmp_code的内容有关。 'α'不是ASCII字符。 – wong2 2011-01-14 14:38:54
你的环境是什么?你能粘贴完整的代码吗? – 2011-01-14 14:45:08
令人惊叹。 char_array在some_func()函数的堆栈上,您提供它并返回指针。据我所知,printf可以打印所有的东西。 尝试在%c%c%c之前打印%s,您可能会有第二次打印的奇怪行为... – 2011-01-14 15:29:31