博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决错误:左值必须作为赋值的左操作数
阅读量:2510 次
发布时间:2019-05-11

本文共 4378 字,大约阅读时间需要 14 分钟。

In this tutorial you will know about one of the most occurred error in C and C++ programming, i.e. lvalue required as left operand of assignment.

在本教程中,您将了解C和C ++编程中最常见的错误之一,即, 左值是赋值的左操作数。

lvalue means left side value. Particularly it is left side value of an assignment operator.

左值表示左侧值。 特别是赋值运算符的左侧值。

rvalue means right side value. Particularly it is right side value or expression of an assignment operator.

rvalue表示右侧值。 特别是右侧值或赋值运算符的表达式。

Example:

例:

a = b + 5;

In above example is lvalue and b + 5 is rvalue.

在上面的示例中, a为左值, b + 5为右值。

In C language lvalue appears mainly at four cases as mentioned below:

在C语言中,左值主要出现在以下四种情况下:

  1. Left of assignment operator.

    赋值运算符的左侧。
  2. Left of member access (dot) operator (for structure and unions).

    成员访问(点)运算符的左侧(用于结构和联合)。
  3. Right of address-of operator (except for register and bit field lvalue).

    运算符地址的权利(寄存器和位字段左值除外)。
  4. As operand to pre/post increment or decrement for integer lvalues including Boolean and enums.

    作为整数(包括布尔值和枚举)的左/前递增或递减操作数。

解决错误:左值必须作为赋值的左操作数 (Solve error: lvalue required as left operand of assignment)

Now let see some cases where this error occur with code.

现在,让我们来看一些在代码中发生此错误的情况。

Example 1:

范例1:

#include
 int main(){    int a =5,b=5;     if(a%b=0)        printf("its crazy");     return 0;}

When you will try to run above code, you will get following error.

当您尝试运行以上代码时,将出现以下错误。

lvalue required as left operand of assignment

Solution: In if condition change assignment operator to comparison operator, as shown below.

解决方案:在条件更改为比较运算符的情况下,如下所示。

#include
 int main(){    int a =5,b=5;     if(a%b==0)        printf("its crazy");     return 0;}

Example 2:

范例2:

#include
 int findFact(int n){    int ans=1,i;        for(i=1;i<=n;i++){ ans*i=ans; }  return ans;} int main(){ int n=5; int fact=findFact(n); printf("%d",fact);  return 0;}

Above code will show the error: lvalue required as left operand of assignment operator.

上面的代码将显示错误:需要左值作为赋值运算符的左操作数。

Here problem occurred due to wrong handling of short hand operator (*=) in findFact() function.

由于在findFact()函数中对短手运算符(* =)的错误处理,导致出现此问题。

Solution: Just by changing the line ans*i=ans to ans*=i we can avoid that error. Here short hand operator expands like this, ans=ans*i. Here left side some variable is there to store result. But in our program ans*i is at left hand side. It’s an expression which produces some result. While using assignment operator we can’t use an expression as lvalue.

解决方案 :只需将ans * i = ans行更改为ans * = i ,就可以避免该错误。 在这里,短手运算符像ans = ans * i这样展开。 在这里,左侧有一些变量可以存储结果。 但是在我们的程序中, ans * i位于左侧。 这是一个产生一些结果的表达式。 使用赋值运算符时,我们不能将表达式用作左值。

The correct code is shown below.

正确的代码如下所示。

#include
 int findFact(int n){    int ans=1,i;        for(i=1;i<=n;i++){ ans*=i; }  return ans;} int main(){ int n=5; int fact=findFact(n); printf("%d",fact);  return 0;}

Example 3:

范例3:

// misunderstanding ternary operator#include
 int main(){ int a=5,b;  a>=5?b=10:b=19;  return 0;}

Above code will show the same lvalue required error.

上面的代码将显示相同的左值必需错误。

Reason and Solution: Ternary operator produces some result, it never assign values inside operation. It is same as a function which has return type. So there should be something to be assigned but unlike inside operator.

原因和解决方案:三元运算符产生一些结果,它从不在运算中分配值。 它与具有返回类型的函数相同。 因此,应该分配一些东西,但不同于内部运算符。

The correct code is given below.

正确的代码如下。

#include
 int main(){ int a=5,b;  b = a>=5? 10: 19;  return 0;}

避免此错误的一些注意事项 (Some Precautions To Avoid This Error)

There are no particular precautions for this. Just look into your code where problem occurred, like some above cases and modify the code according to that.

没有特别的预防措施。 只需查看发生问题的代码(例如上述情况),然后根据该代码进行修改。

Mostly 90% of this error occurs when we do mistake in comparison and assignment operations. When using pointers also we should careful about this error. And there are some rare reasons like short hand operators and ternary operators like above mentioned. We can easily rectify this error by finding the line number in compiler, where it shows error: lvalue required as left operand of assignment.

当我们在比较和赋值操作中犯了错误时,通常会发生90%的错误。 当使用指针时,我们也应该注意这个错误。 并且有一些罕见的原因,例如上述的短手算子和三元算子。 我们可以通过在编译器中找到显示错误的行号轻松纠正此错误:需要左值作为赋值的左操作数。

on Assigncode.com, that provides homework ecxellence in every technical assignment.

在Assigncode.com上进行 ,在每个技术作业中都能提供家庭作业感。

Comment below if you have any queries related to above tutorial.

如果您对以上教程有任何疑问,请在下面评论。

翻译自:

转载地址:http://jqggb.baihongyu.com/

你可能感兴趣的文章
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>
VNPY - CTA策略模块策略开发
查看>>
VNPY - 事件引擎
查看>>