相关疑难解决方法(0)

C中止陷阱6错误

我有这个代码:

void drawInitialNim(int num1, int num2, int num3)
{
    int board[2][50]; //make an array with 3 columns
    int i; // i, j, k are loop counters
    int j;
    int k;

    for(i=0;i<num1+1;i++)      //fill the array with rocks, or 'O'
        board[0][i] = 'O';     //for example, if num1 is 5, fill the first row with 5 rocks
    for (i=0; i<num2+1; i++)
        board[1][i] = 'O';
    for (i=0; i<num3+1; i++)
        board[2][i] = 'O';

    for (j=0; j<2;j++) {       //print the array
      for (k=0; k<50;k++) {
         printf("%d",board[j][k]); …
Run Code Online (Sandbox Code Playgroud)

c arrays loops abort

19
推荐指数
2
解决办法
10万
查看次数

C中的"中止陷阱:6"错误?

我是C的初学者,但我在终端上通过gcc在xcode上运行此代码:

#include <stdio.h>
#include <string.h> 
int main(){
    char name[12] = "Roman Mirov"; 
    printf("My name is %s\n", name);
    name[8] = 'k'; 
    printf("My name is %s\n", name);
    char greeting[] = "hello"; 
    printf("%s %s\n", greeting, name);
    strcpy(greeting, "greetings, "); 
    printf("%s%s\n", greeting, name);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它输出这个:

My name is Roman Mirov
My name is Roman Mikov
hello Roman Mikov
Abort trap: 6
Run Code Online (Sandbox Code Playgroud)

我的问题确切地说,为什么它产生错误而不是显示最后一行作为输出"问候,罗马Mikov"?

c arrays strcpy

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

arrays ×2

c ×2

abort ×1

loops ×1

strcpy ×1