1

In the following code snippet:

for (int column = 0; column < d - 1; column++);
{
    board[&row][&column] == counter;
    counter++;

make returns the following error:

fifteen.c:140:19: error: use of undeclared identifier 'row'
            board[row][column] == counter;
fifteen.c:140:24: error: use of undeclared identifier 'column'
            board[row][column] == counter;
fifteen.c:138:55: error: for loop has empty body [-Werror,-Wempty-body]
        for (int column = 0; column < d - 1; column++);

I don't understand why I get these errors. Please someone help. I'm a n00b.

4

The problem is the semicolon on line 138:

for (int column = 0; column < d - 1; column++);

In C, a semicolon is used to terminate a statement. The body of the for loop (the part in curly braces) is part of the loop statement. If you add a semicolon after the declaration, but before the curly braces, the compiler considers the loop statement to be terminated before the body even starts, producing the for loop has empty body warning.

Now, the loop variable column that you declared in line 138 is only in-scope during the body of the loop. Since you accidentally terminated the loop before giving it a body, column leaves scope immediately after the declaration on line 138. Since you haven't previously declared column in the scope outside of the loop, you get the use of undeclared identifier error.

If you delete the semicolon from the end of line 138 it should fix at least two of those errors; since row is also raising an error, it's likely you have the same problem in an earlier loop declaration. In general, you don't need to use semicolons after for loop declarations or body blocks in C. You only need them in the declaration itself and on the individual statements inside the body. Like this:

for (int i = 0; i < 5; i++)
{
    printf('%i\n', i);
}

Notice there is no semicolon at the end of the loop declaration or after the curly brace that closes the loop body.

| improve this answer | |
1

The above user has identified the syntax errors in your code and also explained the reason.Here is some more info on the error:

Most of the time use of an undeclared identifier error occurs when you try to use a variable without first declaring it. To declare a variable one must write its type followed by its name (also known as identifier). Once you've declared it, only then you are allowed to use it in expressions or assignments.For example:

    int num; //variable declaration
    num=10;

The above code will not give an error but if you skip the declaration part,most probably the compiler will through a use of an undeclared identifier error.

Another way this error pops up is when you try to access a variable outside the block(a piece of code written between curly braces { } ) where it was originally defined.This is the case when you define a variable in a function body or a loop body and try to access it outside the loop or function.

Here, the compiler will throw an error because for it these variables cease to exist once the block has executed.Such variables are called local variables.The code outside their respective block is said to be outside of their scope. You can read more about this when you study C-Scope rules.Here's a useful link on Scope rules in C: http://www.geeksforgeeks.org/scope-rules-in-c/

| improve this answer | |

You must log in to answer this question.

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