This is a syntax error. No problem using addresses:
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; const int cx = 3; cx = 4;
In C ++, this may be legal if SomeType::operator!(int) returns a link:
SomeType answer; !answer = 42;