Expression must be a modifiable lvalue ошибка

I have this following code:

int M = 3; 
int C = 5; 
int match = 3;
for ( int k =0; k < C; k ++ )
{
    match --; 
    if ( match == 0 && k = M )
    {
         std::cout << " equals" << std::endl;
    }
}

But it gives out an error saying:

Error: expression must be a modifiable value

on that «if» line. I am not trying to modify «match» or «k» value here, but why this error? if I only write it like:

if ( match == 0 )

it is ok. Could someone explain it to me?

Brian Tompsett - 汤莱恩's user avatar

asked Oct 5, 2012 at 11:45

E_learner's user avatar

2

The assignment operator has lower precedence than &&, so your condition is equivalent to:

if ((match == 0 && k) = m)

But the left-hand side of this is an rvalue, namely the boolean resulting from the evaluation of the sub­expression match == 0 && k, so you cannot assign to it.

By contrast, comparison has higher precedence, so match == 0 && k == m is equivalent to:

if ((match == 0) && (k == m))

answered Oct 5, 2012 at 11:49

Kerrek SB's user avatar

Kerrek SBKerrek SB

462k92 gold badges872 silver badges1079 bronze badges

3

In C, you will also experience the same error if you declare a:

char array[size];

and than try to assign a value without specifying an index position:

array = ''; 

By doing:

array[index] = '0';

You’re specifying the accessible/modifiable address previously declared.

answered Apr 17, 2015 at 14:20

Alan's user avatar

AlanAlan

1,4593 gold badges20 silver badges36 bronze badges

0

You test k = M instead of k == M.
Maybe it is what you want to do, in this case, write if (match == 0 && (k = M))

answered Oct 5, 2012 at 11:48

tomahh's user avatar

tomahhtomahh

13.4k3 gold badges49 silver badges70 bronze badges

0

Remember that a single = is always an assignment in C or C++.

Your test should be if ( match == 0 && k == M )you made a typo on the k == M test.

If you really mean k=M (i.e. a side-effecting assignment inside a test) you should for readability reasons code if (match == 0 && (k=m) != 0) but most coding rules advise not writing that.

BTW, your mistake suggests to ask for all warnings (e.g. -Wall option to g++), and to upgrade to recent compilers. The next GCC 4.8 will give you:

 % g++-trunk -Wall -c ederman.cc
 ederman.cc: In function ‘void foo()’:
 ederman.cc:9:30: error: lvalue required as left operand of assignment
          if ( match == 0 && k = M )
                               ^

and Clang 3.1 also tells you ederman.cc:9:30: error: expression is not assignable

So use recent versions of free compilers and enable all the warnings when using them.

answered Oct 5, 2012 at 11:48

Basile Starynkevitch's user avatar

Introduction

You may encounter the following message if you move code to another version of the IAR C/C++ Compiler:

Error[Pe137]: expression must be a modifiable lvalue

This message occurs because a cast does not produce an lvalue (that is, a value that can be used on the left side of an assignment).

Also note that a casted expression that is used with the * operator does produce an lvalue, which is why the following is OK:

void f (void * ptr)
{
   *(long *)ptr = 0x11223344L;
}

How to get rid of the error message

There are a couple of ways to rework the code to avoid this error message.

The best way is probably to rework the types so that the cast do not appear in the first place, if possible.

An alternative is to use a temporary variable of the desired type.

The following example fails:

void f (void *ptr)
{
   ((short *)ptr)++; // error
}

You can rewrite it to:

void f (void *ptr)
{
   short *temp = ptr;
   temp++;
   ptr = temp;
}

Technical details

The reason why it does not work is that a cast does not produce an lvalue (ISO/ANSI 9899-1990 6.3.4 and Annex B which describes the syntax of C).

From ISO/ANSI 9899-1990 6.3.4, cast operators, footnote 44: «A cast does not yield an lvalue. […]».

An extract from Annex B follows below:

unary-expression:
postfix-expression
++ unary-expression
-- unary-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )

unary-operator: one of
& * + - ~ !

cast-expression:
unary-expression
( type-name ) cast-expression

conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression

assignment-expression:
conditional-expression
unary-expression assignment-operator assignment-exression

assignment-operator: one of 
= *= /= %= += -= <<= >>= &= ^= |=

The assignment requires a unary-expression on the left side. A cast-expression is not part of a unary-expression. As mentioned above, you can use a unary-operator before the cast-expression to get a unary-expression, such as ‘*‘.

Many compilers permit cast expressions on the left side of an assignment, including several produced by IAR Systems. We have since then switched to another C parser that is more restrictive on this, and besides, it is not proper ISO/ANSI C code.

All product names are trademarks or registered trademarks of their respective owners.

What causes expression must be a modifiable lvalueAs you use conditional statements in c++ you may come across expression must be a modifiable lvalue error. You are likely to see this error message in most conditional expressions when using the assignment operator (=) instead of the comparison operator (==). In this post, you will learn the cause of this error and how you can solve it.

Contents

  • What Causes Expression Must Be a Modifiable Lvalue
    • – Example One: Triggering Expression Must Be a Modifiable Lvalue in C++
    • – Example Two
    • – Example Three
  • How To Solve This Error
    • – The Solution to Example One
    • – The Solution to Example Two
    • – The Solution to Example Three
  • FAQ Section
    • – What Is the Meaning of Expression Must Be a Modifiable value?
    • – What Does Lvalue Mean?
    • – What Triggers Lvalue Required Error?
    • – What Is an Rvalue and Lvalue in C++?
    • – In C++, What Is a Universal Reference
    • – What Is the Meaning of Lvalue Required as Increment Operand?
    • – Which Operator Can You Use To Find the Lvalue of a Variable?
    • – What Is an Expression in C++
    • – When Should an Expression in C++ Be Modifiable?
  • Conclusion

You will get this error message as a result of an expression not producing an lvalue (value on the left side of an assignment). The “l” in the “lvalue” stands for something on the left side of the (=) operator. It means you are attempting to give the value on the right a place in the memory whose name is on the left.

Therefore, whatever is on the left should denote something that has an allocation in memory and lets you modify it. However, if you accidentally try to assign something that is not a left operand an assignment operator (=) instead of a comparison operator (==), you will get this error.

– Example One: Triggering Expression Must Be a Modifiable Lvalue in C++

Suppose you are trying to compare if the sum of two numbers is equal to the third number in C++. One way you can accomplish this is through conditional statements. Here is an example of how you can trigger this error using an assignment operator instead of a comparison operator.

#include <iostream>
using namespace std;
int main()
{
int X = 2;
int Y = 1;
int Z = 3;
// This is the same as if ( X + Y = Z )
if ( (X + Y) = Z) // Error arising from assignment operator (=)
{
std::cout << ” Comparison match” << std::endl;
}
return 0;
}

In this example, you are assigning Z variable an expression (X+Y). Thus, the compiler is going to throw the lvalue error. Every binary + operator results in rvlaue. So, the expression X + Y =Z is an error. Usually, the binary operator (+) takes high precedence over the assignment operator (=). As such X+Y is an rvalue.

Sometimes an operator may use an lvalue operand but result in rvalue. A good example of such an operator is the unary & operator.

– Example Two

Suppose you now want to do a comparison of values by combining the comparison operator and &&. Here is an example of how you can trigger this error by using the assignment operator instead of the comparison operator.

#include <iostream>
using namespace std;
int main()
{
int X = 2;
int Y = 3;
int Z = 3;
if (X == 2 && Y = Z) // Error arising from assignment operator (=)
{
std::cout << “Comparison is a match” << std::endl;
}
return 0;
}

– Example Three

Suppose you have variable A that you wish to print if the value is equal to a certain value. If in the condition statement you end up providing the value of A as the lvalue instead of A, the c++ compiler will throw this error.

#include <iostream>
using namespace std;
int main()
{
int A = 20;
if (20 = A) // *** If you run this program, this line will throw the error.
{
printf(“The value of A is 20.n”);
}
}

The condition statement uses an expression that tries to assign A to the literal value 20. It will generate the error because 20 is not a valid lvalue.

How To Solve This Error

Since the root cause of the error is the wrong use of the assignment operator when creating conditional statements, the solution is quite easy. You only need to take a quick look at the conditional statement to establish the source of the error.

– The Solution to Example One

Say you want to solve the error you got from the first example where you were comparing the sum of two numbers to a third number. You can easily get rid of this error as follows:

#include <iostream>
using namespace std;
int main()
{
int X = 2;
int Y = 1;
int Z = 3;
// This is the same as if ( X + Y == Z )
if ( (X + Y) == Z) // Solution requires replacing assignment operator (=) with a comparison operator (==)
{
std::cout << ” Comparison is a match” << std::endl;
}
return 0;
}

To resolve this error you need to ensure you do not assign Z variable to an expression X + Y like so (X + Y = Z). Instead, use the comparison operator (==). This simple modification of the code will solve this conditional statement error.

Another thing you should keep in mind is that you need to use brackets to make it easy to understand the code. Also, brackets help you get rid of precedence errors.

– The Solution to Example Two

In example two, you were comparing three numbers a pair at a time. In this example, you introduced the && which has higher precedence than the assignment operator. To solve the error in this example, you need to use the comparison operator instead of the assignment operator. So, the condition should be (X == 2 && Y == Z) to eradicate this error.

#include <iostream>
using namespace std;
int main()
{
int X = 2;
int Y = 3;
int Z = 3;
// The same as if ((X == 2) && (Y == Z))
if (X == 2 && Y == Z) // Solved the error arising from assignment operator (=)
{
std::cout << “Comparison is a match” << std::endl;
}
return 0;
}

– The Solution to Example Three

If you recall, in example three, you mistakenly used the value of A as an lvalue in your conditional expression resulting in this error. To get rid of the error, ensure you properly use operands in the condition statement. Here is how you can solve this error:

#include <iostream>
using namespace std;
int main()
{
int A = 20;
if (A = 20) // *** The erroneus (20 = A) has been solved. If you run this program, this line will not throw the error.
{
printf(“The value of A is 20.n”);
}
}

FAQ Section

– What Is the Meaning of Expression Must Be a Modifiable value?

The error indicates that the lvalue (the value on the left-hand side) in a conditional statement is not modifiable. It means you are attempting to assign something to an operand that does not belong to the left side of the assignment operator (=).

– What Does Lvalue Mean?

In c++, an lvalue is a value permitted to be on the left-hand side of the assignment operator (=). The lvalue allows examination or alteration of the designated object. However, there are object types you cannot modify such as arrays, const-qualified, and incomplete types. As such, these values cannot appear on the left side of the assignment operator.

For instance, the variable A can be lvalue that you can modify to be 20 using the assignment operator. However, note that you cannot use 20 as the lvalue for A. Usually, the lvalue is addressable and assignable.

– What Triggers Lvalue Required Error?

The error arises when you put constants as lvalue and variables on the right-hand side of the assignment operator. It means you are unable to assign a value to constants or anything without a memory address. In essence, to assign a value you need a variable.

– What Is an Rvalue and Lvalue in C++?

In c++ expressions, both rvalues and lvalues are important. In simple terms, the lvalue acts as an object reference while the rvalue is the value. The differences between rvalues and lvalues appear when you start writing expressions.

– In C++, What Is a Universal Reference

The && in type declarations showcase either a universal reference or rvalue reference. A universal value in c++ can either be an lvalue or rvalue reference.

– What Is the Meaning of Lvalue Required as Increment Operand?

Usually, any pre-increment operator demands an lvalue as an operand. If it is not present, the compiler will throw this error. Both decrement and increment operators must update the operand at every sequence point hence the need for an lvalue. Unary operators like + and – do not need an lvalue as an operand. Thus, -(++i) expression is valid.

– Which Operator Can You Use To Find the Lvalue of a Variable?

The assignment operator (=) holds the value of a variable. In other words, it assigns a variable its value.

– What Is an Expression in C++

In c++, an expression denotes a statement that contains both a data type and a value. For instance, a declaration statement defines a variable. You can think of it as a holding tank for a value such as a character or a number.

– When Should an Expression in C++ Be Modifiable?

In all conditional expressions especially when using the assignment operator (=) instead of the comparison operator (==).

Conclusion

If you get the lvalue error from a c++ compiler, you should look out for the following tell-tale signs:

  • Check the conditional statements in your program
  • Establish if you are using the assignment operator (=) instead of a comparison operator (==)
  • Check if you are correctly applying the operator precedence in your expression
  • Use brackets in expressions and comparisons to avoid mistakes

Expression must be a modifiable lvalue errorsThat’s it! Whenever a c++ compiler throws the lvalue error, you can now easily pinpoint and solve it.

  • Author
  • Recent Posts

Position is Everything

Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team

Position is Everything

Normally these type of c++ error occurs in conditional statments. For any conditional expression if we are using assignment operator(=) in-stand of comparison (==) then mostly c++ expression must be a modifiable lvalue error occurs.

#include <iostream>
using namespace std;

int main()
{
    int A = 0; 
    int B = 1;
    int C = 1;

    // It is same as if ( (A + B) = C )
    if ( A + B = C ) // ERROR
    {
         std::cout << " Comparison match" << std::endl;
    }
        return 0;
}

Output:

error: lvalue required as left operand of assignment c++ expression must be a modifiable lvalue
error: lvalue required as left operand of assignment

Here we are assigning C variable to expression (A+B) so compiler is giving error of lvalue required as left operand of assignment. if we want to resolve this error then we have to use comparison (==) operator. Also always make sure to add brackets “(” and “)” for easy understanding and also to avoid any operator precedence errors.

SOLUTION :

    if ( (A + B) == C ) // SOLUTION using (==)
    {
         std::cout << " Comparison match" << std::endl;
    }

By modifying this code we can easily resolve lvalue required as left operand of assignment error from condition.

2) c++ expression must be a modifiable lvalue (Scenario-2)

#include <iostream>
using namespace std;

int main()
{
    int A = 0; 
    int B = 1;
    int C = 1;
    
    // Same as if ( (A == 0 && B) = C )
    if ( A == 0 && B = C ) // ERROR
    {
         std::cout << " Comparision match" << std::endl;
    }
        return 0;
}

Output:

c++ expression must be a modifiable lvalue
error: lvalue required as left operand of assignment

SOLUTION :

    if ( (A == 0) && (B == C) ) // SOLUTION using (==)
    {
         std::cout << " Comparision match" << std::endl;
    }

Here assignment(=) operator has lower precedence then &&. So if we change assignment operator to comparison (==) operator then it has higher precedence so (A==0) && (B==C) condition will work proper and expression must be a modifiable lvalue error will be resolved.

CONCLUSION:

Whenever your c++ compiler get error for expression must be a modifiable lvalue, you always need to check below points

  1. Check Assignment (=) operator is used in stand of comparison operator (==)
  2. Check Operator precedence for expression is correctly applied
  3. Try to use bracket for each expression and comparisons for avoiding mistakes

SEE MORE:

What is c++ used for? | Top Uses of C++ programming language

Reader Interactions

I keep getting this error. Can someone please tell me what im doing wrong. Thanks! I am trying to fill an array of struct door.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 //Read array. Put them in a file. Read the file and fill an array of struct door
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std; 
void readatof(int x[], int y[], int n);
struct door
{
	int length[10], width[10];
};
int main()
{
	int length[]={10,20,30,40,50,60,70,80,90,100};
	int width[]={5,10,15,20,25,30,35,40,45,50};

	readatof(length, width, 10);

	fstream arrayFile;
	arrayFile.open("C://Users//Esmeralda//Desktop//arrays.txt",ios::in);
	if (arrayFile.fail())
	{
		cout << "Error openning file" << endl;
		exit(0);
	}

	door tab[10];
	arrayFile >> length[10] >> width[10];
	for(int i=0;i<10;i++)
		{
			tab[i].length = length; //error: Expression must be a modifiable lvalue
			tab[i].width = width;  //error: Expression must be a modifiable lvalue
			arrayFile >> length[10] >> width[10];
		}

	arrayFile.close();
	system("pause");

	return 0;

}	
void readatof(int x[], int y[], int n)
{
	fstream arrayFile;
	arrayFile.open("C://Users//Esmeralda//Desktop//arrays.txt",ios::out);
	if (arrayFile.fail()){exit(0);}
	for (int i=0;i<n;i++)
	{
		arrayFile << x[i] << "t" << y[i] << endl;
	}

	arrayFile.close();
}

  • F000 ошибка kyocera 2040dn
  • F000 kyocera ошибка как исправить
  • F000 kyocera ошибка p3055
  • F000 kyocera 4200 ошибка
  • F00 ошибка протерм скат