0
#include<stdio.h>
#include<ctype.h>

int main() {

  char* start = "There are no";
  char* d = start;
  char* s = d;

  while (s) {
    char c = *s++;
    if (ispunct(c) || isspace(c)) {
      continue;
    }
    *d++ = c;
  }

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

}

我是 c/c++ 的新手,并试图了解如何操作字符串。上面的代码扫描字符串并跳过标点符号和空格并打印没有任何标点符号和空格的字符串。

当我运行它时,我得到“总线错误:10”

我究竟做错了什么?

4

2 回答 2

1

startstring literal,它是隐式的const,修改它会调用未定义的行为。尝试:

char start[] = "There are no";

或者只使用字符串:

std::string start("There are no");
于 2013-11-15T23:30:05.463 回答
1

您正在检查循环条件中的错误内容。你应该检查一下*ss是一个指针,它几乎不会出现0在你的代码中。最终,您进入了一个未映射的内存区域,这会导致SIGBUS.

于 2013-11-15T23:30:32.187 回答