错误:进程以退出代码138(被信号10:SIGBUS中断)结束。

huangapple go评论61阅读模式
英文:

Error: Process finished with exit code 138 (interrupted by signal 10: SIGBUS)

问题

我正在学习C编程,我编写了一个函数,并想看看它是如何工作的。
以下是代码片段:

#include <stdio.h>

void squeeze(char s[], int c) {
    int i, j;
    for (i = j = 0; s[i] != '
#include <stdio.h>

void squeeze(char s[], int c) {
    int i, j;
    for (i = j = 0; s[i] != '\0'; i++) {
        if (s[i] != c)
            s[j++] = s[i];
    }
    s[j] = '\0';
    printf("Converted: %s", s);
}

int main() {
    squeeze("abcdefghigk", 'c');
}
'
; i++) {
if (s[i] != c) s[j++] = s[i]; } s[j] = '
#include <stdio.h>

void squeeze(char s[], int c) {
    int i, j;
    for (i = j = 0; s[i] != '\0'; i++) {
        if (s[i] != c)
            s[j++] = s[i];
    }
    s[j] = '\0';
    printf("Converted: %s", s);
}

int main() {
    squeeze("abcdefghigk", 'c');
}
'
;
printf("Converted: %s", s); } int main() { squeeze("abcdefghigk", 'c'); }

但是当我运行它时,收到了以下错误:
进程以退出码138(被信号10:SIGBUS中断)结束

我使用的是MacBook M1,OS 13.4,Clion C23标准。

期望的结果是:
abdefghigk

但终端中没有显示任何内容,我在传递参数时出错了吗?

我已经查看了这个问题,它看起来很相似,但我不太理解答案,有人能帮忙解释一下吗?

英文:

I'm learning C programming and I wrote a function and wanted to see how it work.
Here is the snippet:

#include &lt;stdio.h&gt;

void squeeze(char s[], int c) {
    int i, j;
    for (i = j = 0; s[i] != &#39;
#include &lt;stdio.h&gt;
void squeeze(char s[], int c) {
int i, j;
for (i = j = 0; s[i] != &#39;\0&#39;; i++) {
if (s[i] != c)
s[j++] = s[i];
}
s[j] = &#39;\0&#39;;
printf(&quot;Converted: %s&quot;, s);
}
int main() {
squeeze(&quot;abcdefghigk&quot;, &#39;c&#39;);
}
&#39;; i++) { if (s[i] != c) s[j++] = s[i]; } s[j] = &#39;
#include &lt;stdio.h&gt;
void squeeze(char s[], int c) {
int i, j;
for (i = j = 0; s[i] != &#39;\0&#39;; i++) {
if (s[i] != c)
s[j++] = s[i];
}
s[j] = &#39;\0&#39;;
printf(&quot;Converted: %s&quot;, s);
}
int main() {
squeeze(&quot;abcdefghigk&quot;, &#39;c&#39;);
}
&#39;; printf(&quot;Converted: %s&quot;, s); } int main() { squeeze(&quot;abcdefghigk&quot;, &#39;c&#39;); }

But when I run it, I received this:
Process finished with exit code 138 (interrupted by signal 10: SIGBUS)

I'm using macbook M1, OS13.4, Clion C23 standard.

Result expected:
abdefghigk

But nothing showed up in the terminal, did I make mistakes when pass the arguments?

I have reviewed this issue, it looks similiar, but I'm not quite understand the answers, could someone please help explain?

答案1

得分: 0

修改字符串字面量是未定义行为。

来自C标准(6.4.5 字符串字面量)

7 不确定这些数组是否不同,只要它们的元素具有适当的值即可。如果程序尝试修改这样的数组,则行为是未定义的。

使用数组来表示字符串:

char input[] = "abcdefghigk";
squeeze(input, 'c');
英文:

It is undefined behavior to modify a string literal.

From the C Standard (6.4.5 String literals)

> 7 It is unspecified whether these arrays are distinct provided their
> elements have the appropriate values. If the program attempts to
> modify such an array, the behavior is undefined.

Use an array for the string:

char input[] = &quot;abcdefghigk&quot;;
squeeze(input, &#39;c&#39;);

huangapple
  • 本文由 发表于 2023年7月12日 23:11:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定