0

I am having a problem compiling fifteen.c. When I try to compile, I get an error in the move function saying:

fifteen.c:225:27: error: expected expression
                coord_t = {i, j};
                          ^
fifteen.c:229:27: error: expected expression
                coord_z = {i, j};
                          ^

A selection of my code from the move function is below. Note that I use a zero to represent the blank:

bool move(int tile)
    {
        // define coordinate variables: (row#, column#)
        int coord_t[2];
        int coord_z[2];
        // find coordinates of tile and blank(i.e. zero)
        for (int i = 0; i < d; i++)
        {
            for (int j = 0; j < d; j++)
            {
                if (board[i][j] == tile)
                {
                    coord_t = {i, j};
                }
                if (board[i][j] == 0)
                {
                    coord_z = {i, j};
                }
            }
        }

Beyond the two error messages displayed above, I also get a third error message from some code later on (in the Won function) but I believe that's a separate issue. If it is a separate issue, please ignore it (or even edit it out or tell me to). I mention it just in case it is not a separate issue. Here's the third error message:

fifteen.c:266:24: error: comparison between pointer and integer ('int *' and 'int')
      [-Werror]
        if (!(board[i] == i))
              ~~~~~~~~ ^  ~

Here's the code relevant to the third error message:

for (int i = 0; i < d; i++)
{
    if (!(board[i] == i))
    {
        return false;
    }
return true;

What I've done: I've looked through a bunch of the move function questions on here but most seem to be taking a different approach and are not concerned with compiling errors. I looked through some of the Expected Expression error questions, but those seem to be caused by other issues (e.g. mismatched braces or repeated declarations of the same loop variable) different from whatever the issue is here.

1 Answer 1

1

These are all syntax errors. In the case of the first two, it's an incorrect initialization, more or less. I'll explain. in order to use the {...} technique, you have to do this at the same time as the array is created.

             int array[] = { 3 , 5 }

would create an array of 2 integers and populate them with 3 and 5. However,

             int array[2];
             array = { 3 , 5 }

would produce the errors you are seeing. If done as two separate steps, like this, it is necessary to explicitly assign the values to each element. It looks like the code is trying to use this initialization technique to reassign all the values of the array at once. It doesn't work that way. Each element of the array must be individually assigned.

The last error is in a similar category. Look at the code:

    if (!(board[i] == i))

The problem here is that board[][] is a two dimensional array. The code above only specifies one dimension, which has the effect of selecting the address of the beginning of the i'th row, not a particular element. Both elements must be specified.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Yikes I should've caught that last error
    – Smithey
    Jun 6, 2017 at 5:38

You must log in to answer this question.

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