Why is my conditional statement giving me an error?

int a=2, b=22; a>b?a:b=222; 

This code gives me an error. I think this is because the conditional operator returns a or b based on the condition, so the value cannot be used as an Lvalue . In my case, 22 = 222, so this is a mistake.

 int *i, *j, a=2,b=222; i = &a; j = &b; a>b?i:j=&a; 

In this case, the compiler also throws an error. According to my assumption, as in the previous case, here it returns the value i or j . So 0x123 = & a; [let 0x123 is the address stored in j]. Invalid?

At that moment I did a search . And I tried 0x123 = &a; , but the compiler still throws an error. I think that the compiler will not allow me to change memory locations for security reasons.

Please let me know that all my assumptions are correct and tell me what kind of security error will occur if the programmer is allowed to manipulate the memory cells. I assume that the memory cell may contain a value that should not be changed.

EDIT Error error: lvalue required as left operand of assignment .

+8
c conditional-operator
source share
2 answers

This is a syntax error. No problem using addresses:

 #include <stdio.h> int main() { int a=2,b=4; *(b>2 ? &a : &b) = 7; printf("a = %d; b = %d\n", a, b); return 0; } 
Syntax

C does not allow the use of the ?: Expression on the left side of the assignment operator. The version above is syntactically correct, because the expression on the left side of the assignment is an dereference expression with an argument in parentheses.

The only way to assign via pointer is to use *ptr = val; . It does not matter how the pointer is calculated if it is a valid pointer to the type of the right-hand side. It may even be a constant, but casting to a pointer - if you have some way of knowing that the constant was a valid address: *(int*)(0x124) = 42; . But you always need the dereference operator to indicate that you are assigning it through a pointer.


Detailed grammar explanation:

There are two corresponding grammar pieces that demonstrate the difference between C and C ++ relative to the original expression ('a> b? A: b = 222`)

In C, we have (s sect; s 6.5.15-16): (in italics)

 
      conditional-expression: 
          logical-OR-expression 
          logical-OR-expression?  expression: conditional-expression 

      assignment-expression: 
          conditional-expression 
          unary-expression assignment-operator assignment-expression 
 

In C ++, equivalent products (& sect; & sect; 5.16-17): (emphasis added)

 
      conditional-expression: 
          logical-or-expression 
          logical-or-expression?  expression: assignment-expression 

      assignment-expression: 
          conditional-expression 
          logical-or-expression assignment-operator initializer-clause 
          throw-expression 
 

Note that in C, the only thing that can occur before an assignment operator is a unary expression, so there should be anything as complex as parental expression in parentheses. In addition, the right operand of the conditional expression must be a conditional expression, which means that the assignment expression is invalid.

In C ++, on the contrary, the assigned operand in an assignment expression can be a logical expression or one that still means that the conditional expression is invalid, but allows other possibilities (many of which are useful only when operators are overloaded). But a C ++ conditional expression can have an assignment expression as its rightmost operand.

So, in C, a>b?a:b=222 is a syntax error. It cannot be manufactured by any production for expression . But in C ++, the same expression is legal, and the right operand of the conditional operator is b=222 ; in other words, it matches a>b?a:(b=222) .

It should be noted that just because the expression matches the syntax of the language does not mean that the expression is legal. The left side of the assignment operator must be a "variable". Therefore, none of the following syntactically correct expressions is legal:

 int x; -x = 3; /* lhs is not an lvalue */ const int cx = 3; cx = 4; /* lhs is not modifiable */ 

In C ++, this may be legal if SomeType::operator!(int) returns a link:

 SomeType answer; !answer = 42; 
+12
source share
 int a=2, b=22; a>b?a:b=222; 

I ran your code in GCC version 4.8.1 it worked fine. Then I changed it to the next to clear your view.

 #include <stdio.h> main() { int a=2, b=22; printf("answer: %d",a>b?a:b=222 ); } 

he gives the next exit

answer: 222

 a>b?a:b=222; 

above line can be written as

 (a>b)?a:(b=222); 

when we run it, 222 is assigned to the variable b first, and then ?: the statement is executed.

-one
source share

All Articles