-1

So I'm trying to check my functions return value in an if statement so that i can check wheter it's working fine or not.

I have:

    if (int r = (input(num_first, num_second, fixPTR)) =! -1)
    {
// do smth
    }

but Visual Studio says : expression must be a modifiable lvalue

how do i fix it??

Jongware
  • 22,200
  • 8
  • 54
  • 100
vvvsg
  • 401
  • 1
  • 7
  • 15
  • 3
    You cannot declare a variable in an `if` controlling expression. – ouah Apr 15 '15 at 10:32
  • Declare `r` outside the `if`. – Maroun Apr 15 '15 at 10:32
  • 1
    Check your basic C syntax. `!-1` is always `0` and is valid, but I doubt that's what you intended to check for. – Jongware Apr 15 '15 at 10:34
  • @Quentin show me a valid declaration in an `if` controlling expression and I show you you are wrong. – ouah Apr 15 '15 at 10:53
  • @Quentin This may be a subtle difference between C and C++. I believe the (informal) C grammar does not allow for declarations, but the C++ one appears to (cf. Mike's answer in http://stackoverflow.com/questions/7836867/c-variable-declaration-in-if-expression). – Peter - Reinstate Monica Apr 15 '15 at 11:01
  • I'm writing C code in Visual C++ so i can declare variables in an if statement. – vvvsg Apr 15 '15 at 11:04
  • @ouah My bad ! I did mix up with C++. Looks like I'm not the only one :) – Quentin Apr 15 '15 at 11:44
  • Not sure what you are intending to do, but i believe the != operator has a higher precedence than the = operator, so 'return value' != -1 will evaluate to true or false and t hen be assigned to 'int r' – user2950911 Apr 15 '15 at 12:56

1 Answers1

5

You need to declare your variables before your code.

And the syntax for the opposite of == is != and not =! by the way.

int r;

if ((r = input(num_first, num_second, fixPTR)) != -1)
{
// do smth
}
Aracthor
  • 5,757
  • 6
  • 31
  • 59