鱼C论坛

 找回密码
 立即注册
查看: 3834|回复: 1

这段代码哪错了

[复制链接]
发表于 2012-9-3 16:28:56 | 显示全部楼层 |阅读模式
10鱼币
#include <stdio.h>
int main(void)
{
char * orig = "How are you";
char * pos;


while (*orig++)
{
  if (*orig == ' ')
  {
   pos = orig;
   do
   {
    *pos = *(pos + 1);
    pos++;
   } while (*pos);
  }
}
puts(orig);

puts("Bye!");
return 0;
}
目的是去掉空格字符

最佳答案

查看完整内容

错误主要是没空间放,和指针指到不该指的地方去了。 修正后的代码:
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-9-3 16:28:57 | 显示全部楼层
错误主要是没空间放,和指针指到不该指的地方去了。
修正后的代码:
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. char * orig = "How are you";
  5. char pos[20] = {0};
  6. int i = 0;

  7. while (*orig)
  8. {
  9.    if (*orig != ' ')
  10.    {
  11.     pos[i] = *orig;
  12.         i++;
  13.    }
  14.    orig++;
  15. }
  16. puts(pos);
  17. puts("Bye!");
  18. return 0;
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-14 23:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表