0

Look at this piece of code.. In this I am taking input from a file and assigning to an array sudoku[][].. and simultaneously assigning those values to TempArr[][] (another array)..

But when I checked the values of TempArr[][] after assigning, there aren't same as in sudoku[][].

for (i=0;i<size;i++)
{
    for (j=0;j<size;j++)
    {
        if(fscanf(ip_file,"%d",&sudoku[i][j])==1)
        {         
                  //copy to TempArr
                  TempArr[i][j]==sudoku[i][j];
        }
        else
        {
            perror ("fscanf failed on input file.\n");
            // return error
        }
    }
}

So, when I replaced "sudoku[][]" with "TempArr[][]", its working.. i.e., if(fscanf(ip_file,"%d",&TempArr[i][j])==1)

Why this is happening and how do I handle this situation ?

2
  • Don't you get an expression has no effect kind of warning for that? Commented Nov 16, 2011 at 17:37
  • @K-ballo - it does if you're using -Wunused-value (or -Wall) with gcc, but otherwise ... not so much. However, it's a good point to make - thanks, editing. Commented Nov 16, 2011 at 20:34

2 Answers 2

5
TempArr[i][j]==sudoku[i][j];

See the == ? That wouldn't be assigning ;)

Edit: Also worth noting - the compiler can help you with these things. If you use the -Wall option when compiling, it will tell you:

> gcc -Wall -o test test.c
test.c:13:9: warning: statement with no effect [-Wunused-value]

There's a number of helpful warning levels you can specify to alert you of these things:

http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I wasn't looking at that small mistake
Should there not be a warning for comparisons where the result is ignored? I'm sure I got one last time I did this on gcc. It's such a common error, (we've all done it), that there should be a warning.
Some compilers warn of a result with no side-effect or use. And Java will not let you do the opposite (use = instead of ==) due to the way they type the values.
Not unless you specifically ask for them. It's a good point to make though, so I've added that to my answer.
I didn't get any warning. I'm using Mingw
1

Uh, == isn't assignment, it's a test for equality.

2 Comments

(I've made the same mistake more than once -- probably more often than I've tried to use = for comparison.)
I loath admitting it, but same here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.