Error in C code error: expression is not assignable
Error in C code error: expression is not assignable
我正在编写一个函数,用于打印程序执行的描述。我程序中的一个函数使用 0
作为以 10 为底的数字转换的信号。
我希望我的程序具有友好的输出,并告诉用户数字是否已转换为基数 10,而不是让程序说该数字是从基数 0 转换而来的。
当我尝试编译此代码时,我收到一条错误消息,内容为 'expression is not assignable'。
我正在使用 cc 编译器在命令行上编译
Apple LLVM 版本 7.3.0 (clang-703.0.29)
知道这个错误是什么意思以及如何更正吗?
谢谢。
void foo( int base ){
int theBase;
base == 0 ? theBase = 10: theBase = base;
printf("%s%d\n", "The base is ", theBase)
}
错误信息:
error: expression is not assignable
base == 0 ? theBase = 10: theBase = base;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
因为你需要左值,它所属的地方,在表达式的左边,像这样
theBase = (base == 0) ? 10 : base;
注意编译器是如何考虑的
base == 0 ? theBase = 10 : theBase
就像那个表达式中的“lvalue”,因为运算符优先。
ternary operator 是一个运算符,因此您不能用它来替换 if 语句。
你应该使用
theBase = (base == 0 ? 10 : base);
而不是
base == 0 ? theBase = 10: theBase = base;
你必须在作业周围加上括号
base == 0 ? (theBase = 10) : (theBase = base);
否则优先级在耍你。更好的是,使用惯用语法:
theBase = base ? base : 10;
你在这里做的是条件赋值。
通常你可以这样做:
if (base == 0)
theBase = 10;
else
theBase = base;
此处您选择使用三元表达式。它确实有点像 if/else 结构,但它确实不同。
三元return一个值,它不是用来根据条件执行代码的。不,它 return 一个基于条件的值。
所以在这里,你必须做:
theBase = (base == 0 ? 10 : base);
(括号不是必须的,但能避免错误会好很多)
事实上,你可以让一个三元执行代码,以多种方式,比如return调用一个函数:
int my_function()
{
printf("Code executed\n");
return (10);
}
/* ... */
theBase = (base == 0 ? my_function() : base);
编辑:
是的,您可以使用该代码:
base == 0 ? (theBase = 10) : (theBase = base);
但在这种情况下使用三元组是毫无用处的,因为您仍然需要复制 theBase = X
代码。
没有回答问题,但可能对遇到此问题的其他人有所帮助:
就我而言,我有合法的任务,例如:
int xyz = 5;
我不知道包含的 headers 某处有以下行:
#define xyz 14
我不确定为什么编译器在这个语义错误之前没有大喊语法错误。无论哪种方式,如果有人对这个答案感到沮丧,我会建议检查变量名称之前是否尚未在某处定义为宏或类似名称。
我正在编写一个函数,用于打印程序执行的描述。我程序中的一个函数使用 0
作为以 10 为底的数字转换的信号。
我希望我的程序具有友好的输出,并告诉用户数字是否已转换为基数 10,而不是让程序说该数字是从基数 0 转换而来的。
当我尝试编译此代码时,我收到一条错误消息,内容为 'expression is not assignable'。
我正在使用 cc 编译器在命令行上编译
Apple LLVM 版本 7.3.0 (clang-703.0.29)
知道这个错误是什么意思以及如何更正吗? 谢谢。
void foo( int base ){
int theBase;
base == 0 ? theBase = 10: theBase = base;
printf("%s%d\n", "The base is ", theBase)
}
错误信息:
error: expression is not assignable
base == 0 ? theBase = 10: theBase = base;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
因为你需要左值,它所属的地方,在表达式的左边,像这样
theBase = (base == 0) ? 10 : base;
注意编译器是如何考虑的
base == 0 ? theBase = 10 : theBase
就像那个表达式中的“lvalue”,因为运算符优先。
ternary operator 是一个运算符,因此您不能用它来替换 if 语句。
你应该使用
theBase = (base == 0 ? 10 : base);
而不是
base == 0 ? theBase = 10: theBase = base;
你必须在作业周围加上括号
base == 0 ? (theBase = 10) : (theBase = base);
否则优先级在耍你。更好的是,使用惯用语法:
theBase = base ? base : 10;
你在这里做的是条件赋值。
通常你可以这样做:
if (base == 0)
theBase = 10;
else
theBase = base;
此处您选择使用三元表达式。它确实有点像 if/else 结构,但它确实不同。
三元return一个值,它不是用来根据条件执行代码的。不,它 return 一个基于条件的值。
所以在这里,你必须做:
theBase = (base == 0 ? 10 : base);
(括号不是必须的,但能避免错误会好很多)
事实上,你可以让一个三元执行代码,以多种方式,比如return调用一个函数:
int my_function()
{
printf("Code executed\n");
return (10);
}
/* ... */
theBase = (base == 0 ? my_function() : base);
编辑:
是的,您可以使用该代码:
base == 0 ? (theBase = 10) : (theBase = base);
但在这种情况下使用三元组是毫无用处的,因为您仍然需要复制 theBase = X
代码。
没有回答问题,但可能对遇到此问题的其他人有所帮助: 就我而言,我有合法的任务,例如:
int xyz = 5;
我不知道包含的 headers 某处有以下行:
#define xyz 14
我不确定为什么编译器在这个语义错误之前没有大喊语法错误。无论哪种方式,如果有人对这个答案感到沮丧,我会建议检查变量名称之前是否尚未在某处定义为宏或类似名称。