反转字符串时 printf 输出错误
printf wrong output when reversing a string
所以这是反转字符串的代码
#include<stdio.h>
char* function(char *);
int main()
{
char a[]="computer";
printf("%s", function(a));
return 0;
}
char* function(char *p)
{
int l,i;
char t;
for (l=0;*(p+l)!='[=10=]';l++);
for(i=0; i<(l/2) ; i++)
{
t=*(p+i);
*(p+i)=*(p+l-1-i);
*(p+l-1-i)=t;
}
return (p);
}
但是如果我将正文中的 printf("%s", function(a));
更改为
printf("%s", function("computer"));
在 dev c++ 中没有输出(输出为空白)....但即使进行了此更改,它也会在 turbo c++ 中提供所需的输出....这是为什么?
您传递给 function
的参数 "computer"
是一个字符串文字,而 changing/manipulating 字符串文字的内容是未定义的行为。这就是您遇到的情况 - 未定义的事情。
所以这是反转字符串的代码
#include<stdio.h>
char* function(char *);
int main()
{
char a[]="computer";
printf("%s", function(a));
return 0;
}
char* function(char *p)
{
int l,i;
char t;
for (l=0;*(p+l)!='[=10=]';l++);
for(i=0; i<(l/2) ; i++)
{
t=*(p+i);
*(p+i)=*(p+l-1-i);
*(p+l-1-i)=t;
}
return (p);
}
但是如果我将正文中的 printf("%s", function(a));
更改为
printf("%s", function("computer"));
在 dev c++ 中没有输出(输出为空白)....但即使进行了此更改,它也会在 turbo c++ 中提供所需的输出....这是为什么?
您传递给 function
的参数 "computer"
是一个字符串文字,而 changing/manipulating 字符串文字的内容是未定义的行为。这就是您遇到的情况 - 未定义的事情。