得到错误“需要左值作为赋值的左操作数”。 [英] Getting error "lvalue required as left operand of assignment"

查看:124
本文介绍了得到错误“需要左值作为赋值的左操作数”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C语言的新手,在编译简单代码时遇到以下问题:

I am a novice in C and having the following problem while compiling a simple code:

#include <stdio.h>

int main()
{
    int i, a, b;
    i = 3;
    a = b = 0;

    printf("Before: ");
    printf("%d %d\n", a, b);
    i == 3 ? a = 4 : a = 10; /* Line 9 */

    printf("After: ");
    printf("%d %d\n", a, b);
    return 0;
}

给我错误:

#gcc some.c
In function ‘main’:
some.c:9: error: lvalue required as left operand of assignment

我听不懂。我在做什么错?

I cannot understand it. What am i doing wrong?

推荐答案

该运算符

i==3 ? a=4 : a = 10;

等同于

( i==3 ? a=4 : a ) = 10;

使用

i==3 ? a=4 : ( a = 10 );

这篇关于得到错误“需要左值作为赋值的左操作数”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆