c - 错误 : expression must be a modifiable lvalue (tal * tal)

标签 c lvalue

所以这是我的问题:

#include "stdafx.h"

int kvad(int tal) {
    int sum;
    tal * tal = sum; /* The left "tal" has the error: Expression must be a modifiable lvalue*/
    return sum;
}

int kub(int alt) {
    int pro;
    alt * alt * alt = pro; /* The left "alt" has the error: Expression must be a modifiable lvalue*/
    return pro;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int ggr, gda, tre, tva;
    printf("Hur många tal att multiplicera: ");
    scanf_s("%d", ggr);
    printf("\n i   i * i   i * i * i\n=== ======= ===========\n");
    for (gda = 1; gda <= ggr; gda++) {
        tva = kvad(gda);
        tre = kub(gda);
        printf("%2d%6d%10d\n", gda, tva, tre);
    }
    return 0;
}

我不知道是否需要最后一部分,但我不确定所以我还是包含了它。

我知道还有其他线程有类似的问题,但我在那里找不到解决方案。

最佳答案

左值 是一个可以赋值的值,通常是一个变量。相比之下,rvalue 是一个不能赋值的值。名称来源于这样一种趋势,即 lvalue 倾向于出现在赋值的左侧,而 rvalue 倾向于出现在赋值的右侧。在您的代码中,您有:

tal * tal = sum;

这是一个错误,因为 tal * tal 产生了一个无法赋值的值。表达式产生一个数字,但这与变量不同,因为将一个数字分配给另一个数字没有意义。这就像说 5 = variable

还要记住赋值是不可交换的。也就是说,tal * tal = sum 不等同于 sum = tal * tal= 左边的名称将被赋予右边的值,因此左边的名称必须始终是可以分配给的东西,即 左值

关于c - 错误 : expression must be a modifiable lvalue (tal * tal),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25512021/

相关文章:

c - 当 proc 目录中没有 cmdline 文件时,如何在 Unix 上检查进程是否正在按名称运行?

c - 为什么我会遇到 isdigit() 的段错误?

c - C中的const总是占用存储空间吗?

c - 最简单的RGB图像格式是什么?

c++ - 左值衰减为右值并出现自动错误

c++ - 双脚本数组作为左值

c - 是否有标准 mtx_t 'invalid' 状态?

c++:函数左值或右值

C++ 错误 : lvalue required as unary '&' operand

c++ - 错误 C2106 : '=' : left operand must be l-value in Fibonacci sequence by dynamic programming in C++