我正在关注一本关于 c 编程的书,遇到了这个例子:
a<=20?b=30:c=30;
我只需要使用一次数字 30。书中的解决方案是
((a<=20)?&b:&c)=30);
当我尝试运行它时,我收到以下错误:
lvalue required as left operand of assignment.
那里好像少了一个括号,但我不知道在哪里。
你能告诉我如何解决这个问题吗?
最佳答案
假设 a 和 b 是相同类型的数字,就这样做
*((a <= 20) ? &b : &c) = 30;
获取 a 和 b 地址需要取消引用('*')以获得 lvalue
来自@Vrintle 的备注你也可以在 C++ 中做
((a<=20)?b:c)=30;
编译器知道表单是一个左值,所以b和c的值没有被使用,而是它们的引用
It seems like one of the parenthesis is missing there, but I don't know where.
在 ((a<=20)?&b:&c)=30);
最后一个')'没有对应的'('
关于c - 表达式 a<=20?b=30 怎么写 :c=30; such that 30 is used only once?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64399972/