[c] Abort trap 6 error in C

I have this code:

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]);
      }
    }
   return;
}

int main()
{
    int numRock1,numRock2,numRock3;
    numRock1 = 0;
    numRock2 = 0;
    numRock3 = 0; 
    printf("Welcome to Nim!\n");
    printf("Enter the number of rocks in each row: ");
    scanf("%d %d %d", &numRock1, &numRock2, &numRock3);
    drawInitialNim(numRock1, numRock2, numRock3); //call the function

    return 0;
}

When I compile this with gcc, it is fine. When I run the file, I get the abort trap 6 error after entering the values.

I have looked at other posts about this error, and they don't help me.

This question is related to c arrays loops abort

The answer is


You are writing to memory you do not own:

int board[2][50]; //make an array with 3 columns  (wrong)
                  //(actually makes an array with only two 'columns')
...
for (i=0; i<num3+1; i++)
    board[2][i] = 'O';
          ^

Change this line:

int board[2][50]; //array with 2 columns (legal indices [0-1][0-49])
          ^

To:

int board[3][50]; //array with 3 columns (legal indices [0-2][0-49])
          ^

When creating an array, the value used to initialize: [3] indicates array size.
However, when accessing existing array elements, index values are zero based.

For an array created: int board[3][50];
Legal indices are board[0][0]...board[2][49]

EDIT To address bad output comment and initialization comment

add an additional "\n" for formatting output:

Change:

  ...
  for (k=0; k<50;k++) {
     printf("%d",board[j][k]);
  }
 }

       ...

To:

  ...
  for (k=0; k<50;k++) {
     printf("%d",board[j][k]);
  }
  printf("\n");//at the end of every row, print a new line
}
...  

Initialize board variable:

int board[3][50] = {0};//initialize all elements to zero

( array initialization discussion... )


Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to loops

How to increment a letter N times per iteration and store in an array? Angular 2 Cannot find control with unspecified name attribute on formArrays What is the difference between i = i + 1 and i += 1 in a 'for' loop? Prime numbers between 1 to 100 in C Programming Language Python Loop: List Index Out of Range JavaScript: Difference between .forEach() and .map() Why does using from __future__ import print_function breaks Python2-style print? Creating an array from a text file in Bash Iterate through dictionary values? C# Wait until condition is true

Examples related to abort

Abort trap 6 error in C How to abort an interactive rebase if --abort doesn't work? How do I abort/cancel TPL Tasks?