为什么 ++ 和 -- 只在变量前后使用?

Why is ++ and -- only used before and after variables?

我正在阅读 Brian W. Kernighan 和 Dennis M. Ritchie 合着的 "The C programming language",第 46 页指出 "The increment and decrement operators can only be applied to variables; an expression like (i+j)++ is illegal"。为什么它不能在变量之前或之后的其他地方使用?

你能做到 8++ 吗?

操作数必须具有算术或指针数据类型,并且必须引用可修改的数据对象。

我不确定 "only before variables" 是什么意思。运算符 ++--(后缀和前缀)需要 可修改的左值 作为它们的操作数。左值不一定由立即变量名表示。

例如,你可以这样做

int a[10] = { 0 };
++*(a + 5);

在你的理解中*(a + 5)是"variable"吗?

i + j 的问题不在于 "not a variable"。 i + j 的问题在于它不是左值。这就是为什么您不能对其应用 ++ 的原因。

在 C 语言中,术语 "variable" 有时用作术语 "modifiable [scalar] object" 的半非正式同义词,后者又与术语 "modifiable lvalue [of scalar type]" 同义。您所指的书可能在半非正式意义上使用了 "variable" 一词。在这个意义上 *(a + 5) 也是 "variable".

因为 (i+j) 是两个变量相加的结果,你没有实际存储 i+j 的变量,它只是一个计算结果,所以这就像说,假设 i = 1,j = 2, i+j 等于 3,而 3++ 无效,因为 3 是右值。如需更多信息,请访问此处,http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c