左操作数必须是l值

时间:2011-11-02 03:01:57

标签: c operand

可以帮助一些人。这两行代码出错。 num_red - count_red = red_pot;// all defined as 0

while (count_red = 0 && count_yellow = 0 && count_green = 0 && count_brown = 0 && count_blue = 0 && count_pink = 0)
        {
            if (count_black = 0)
            {
                score = score + 7;
                printf("Score: %d\n", score);
                num_balls = num_balls - 1;
            }

        }

1 个答案:

答案 0 :(得分:5)

如果这是一种类C语言,则需要使用==进行相等性检查,而不是=。单=用于赋值,以便:

int seven = 7;
int five = 5;
if (seven - five == 2) ...

没关系,但是:

int seven = 7;
int five = 5;
if (seven - five = 2) ...

即使汇编也不会做你期望的事情。

您的代码中有一个经典示例。细分:

if (count_black = 0) blah;
blah为零时,

执行count_black count_black设置为零并坚决拒绝执行blah,因为count_blah = 0的结果为0(错误)。


如果你想要平等:

num_red - count_red == red_pot

确实如此,您需要根据其他两个“已知”变量分配其中一个变量(“未知”变量)。例如,如果已知num_redcount_red,请将red_pot设置为:

red_pot = num_red - count_red;

或者,如果知道red_potcount_red,请将num_red设置为:

num_red = count_red + red_pot;