Search code examples
c++arraysmacosemacsbus-error

Bus error: 10 while trying to mess with arrays


I'm just playing with arrays and I've written this tiny thing in Emacs (OSX) and I'm getting a Bus error: 10 when I try to run it. I don't know why that is, maybe someone can show me...

#include <iostream>

int main()
{    
  int array[3][3];

  for (int i = 0; i < 3; i++)
    {
      for (int j = 0; i < 3; j++)
      {
        array[i][j] = j + i;
      }
    }
  for (int i = 0; i < 3; i++)
    {
      for (int j = 0; j < 3; j++)
      {
        std::cout << array[i][j];
      }
    }
  return 0;
}

Solution

  • Riiiiight here:

      for (int j = 0; i < 3; j++)
    

    I think you want a j there in the middle.

      for (int j = 0; j < 3; j++)