如何用C重写这个三元运算符?
如何在循环中而不是在三元运算符中写入如何用C重写这个三元运算符?,c,conditional-operator,C,Conditional Operator,如何在循环中而不是在三元运算符中写入 temp->status = (inStore ? waiting : called); 会不会是: if (inStore){ return waiting; } else ( return called; } 我不确定,因为我在执行此操作时出错,我在一个无效函数中使用它您必须将等待或调用分配给temp->status变量,而不是返回它,也在否则中错误地使用了括号。整个事情应该是: if (inStore) temp
temp->status = (inStore ? waiting : called);
会不会是:
if (inStore){
return waiting;
}
else (
return called;
}
我不确定,因为我在执行此操作时出错,我在一个无效函数中使用它您必须将
等待
或调用
分配给temp->status
变量,而不是返回它,也在否则
中错误地使用了括号。整个事情应该是:
if (inStore)
temp->status = waiting;
else
temp->status = called;
我不知道为什么在这种情况下要求使用循环,因为不需要使用循环。问题在于:
其他(
)。将(
更改为{
,编译应该是干净的
该语句只是一个快捷语法中的if-then-else
语句,赋值语句以其单个lvalue
表示。因此:
temp->status = (inStore ? waiting : called);
翻译为:
if(inStore == true)
{
temp->status = waiting;
}
else
{
temp->status = called;
}
请注意,您的语法没有任何错误(除了”(“
此处:其他(
)。在函数中,如果函数在离开前不需要清理,则最好使用return
语句。if(inStore)temp->status=waiting;else temp->status=called;
将等同于第一个代码。我看不到与循环的任何关系。
if(inStore){
temp->status = waiting;
} else { // there was a ( instead of a {
temp->status = called;
}