I am trying to complete the first step of game of fifteen - writing the INIT function. I've partially written a function that I think should work. The thing that is bothering me is this: I want to run the function and then print out the contents of the array, and then break the programme (otherwise it will clear the screen and move on to ask for a tile to move). But the programme doesn't break! It doesn't appear to print, either. I have no idea why not. Your help appreciated. I've now updated my code for INIT (i) after spotting some errors and especially after help from Blauelf, and (ii) I've now included a function that changes the penultimate two positions in the array, if the array size is even. The updated code is below:

void init(void)
    // MF Note - fill the whole board with ints from (d-1) to 0.
    int ctr = 1, board[d][d];
    for(int row = 0; row < d; row++)
        for(int col = 0; col < d; col++)
            board [row][col] = ((d*d)-ctr);

    // MF Note - now check whether the array is of even or odd size; and if even, swap array positions (d-2) and (d-3) - i.e. the 1 and 2
    if(d % 2 == 0)
        int temp = board[d-1][d-1];
        board[d-1][d-1] = board[d-1][d-2];
        board[d-1][d-2] = temp;
    // MF Note - now declare position [d-1][d-1] (the bottom right position) is 100 (will have been filled with 0 by the nested for-loops, above)
    board [d-1][d-1] = 100;

    // MF Note - now print out the contents of the array, to check whether it works, then break.
    for(int i = 0; i < d; i++)
        for(int j = 0; j < d; j++)
            printf("%d\n", board[i][j]);

The output I get when I run the programme (e.g. with "./fifteen 3") is: WELCOME TO GAME OF FIFTEEN (wait for 2 seconds; then screen clears and prints:) Tile to move:

Please help - why doesn't the break command get invoked (let alone the printf command)!


2 Answers 2


You use local stack variable for the board, local variables will be deleted on leaving the function, should probably use a global variable. Local variables of same name shadow global ones (compiler flag -Wshadow will warn in that case).

Your single loop would output just the elements of the diagonale, needs nested loops just like when filling, and break leaves the current loop.

  • Many thanks Blauelf. A bit more detail: (1) d is passed as a command line argument; (2) "board" is a global variable, defined at the top of the programme, just after the #include statements. It's initialized as being a 9x9 array; I'm trying to re-set that to a dxd array at this point. Jan 26, 2017 at 16:29
  • You cannot redefine the global variable, you have to use it.
    – Blauelf
    Jan 26, 2017 at 18:04
  • Blauelf is right, redefining board creates something called a "shadow variable", or in this case, a shadow array. Simply put, the local declaration creates a new, local array called board. It takes priority and masks or hides the global array. The local function will use the shadow array and the global array remains untouched. When the function exits, the local array ceases to exist and the focus returns to the global array, which was not changed by the function.
    – Cliff B
    Jan 26, 2017 at 20:27
  • Thanks again Blauelf and Cliff B - I think I finally get it. Effectively, in my fourth line above - int crt=1, board [d][d] - I'm declaring the array "board" again, inside this function. I don't need to do this, since I've already declared it at the beginning of the programme. In fact, if I do this, the array gets used inside this function, but isn't available elsewhere. So what I need to do (and have now done) is just use the array I've already declared globally. Doh! Jan 27, 2017 at 10:54

OK, I think I get it now. After updating the code as above, I get an error when I try to make the file that says (something like): Break function outside of an if or do-while loop. Basically, it's telling me that I can't break something unless it's in a loop, right? Which I had forgotten. So I've removed the break statement, and now my function runs and fills the array per the specification.

Thanks everyone for your help.

  • break and continue influence the loop which they are used in. For leaving a function, there's return, though all of these control only one layer.
    – Blauelf
    Jan 26, 2017 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .