lvalue required as left operand of assignment error when using C++ - c++

int main()
{
int x[3]={4,5,6};
int *p=x;
p +1=p;/*compiler shows error saying
lvalue required as left
operand of assignment*/
cout<<p 1;
getch();
}

When you have an assignment operator in a statement, the LHS of the operator must be something the language calls an lvalue. If the LHS of the operator does not evaluate to an lvalue, the value from the RHS cannot be assigned to the LHS.
You cannot use:
10 = 20;
since 10 does not evaluate to an lvalue.
You can use:
int i;
i = 20;
since i does evaluate to an lvalue.
You cannot use:
int i;
i + 1 = 20;
since i + 1 does not evaluate to an lvalue.
In your case, p + 1 does not evaluate to an lavalue. Hence, you cannot use
p + 1 = p;

Put simply, an lvalue is something that can appear on the left-hand side of an assignment, typically a variable or array element.
So if you define int *p, then p is an lvalue. p+1, which is a valid expression, is not an lvalue.
If you're trying to add 1 to p, the correct syntax is:
p = p + 1;

To assign, you should use p=p+1; instead of p+1=p;
int main()
{
int x[3]={4,5,6};
int *p=x;
p=p+1; /*You just needed to switch the terms around*/
cout<<p<<endl;
getch();
}

if you use an assignment operator but use it in wrong way or in wrong place,
then you'll get this types of errors!
suppose if you type:
p+1=p; you will get the error!!
you will get the same error for this:
if(ch>='a' && ch='z')
as you see can see that I i tried to assign in if() statement!!!
how silly I am!!! right??
ha ha
actually i forgot to give less then(<) sign
if(ch>='a' && ch<='z')
and got the error!!

It is just a typo(I guess)-
p+=1;
instead of p +1=p; is required .
As name suggest lvalue expression should be left-hand operand of the assignment operator.

Related

Why assignment operator = doesn't return the value of pointer but the dereference value?

From What does an assignment return? :
An assignment expression has the value of the left operand after the assignment
and this code:
#include <iostream>
using namespace std;
int main() {
int a[5] = { 0,1,2 };
int* a_ptr = a;
int b = (*a_ptr++ = 3); //int *b won't compile
cout << b << endl; //3
}
What is the left operand of = when evaluating (*a_ptr++ = 3) ?
What's the definition of an operand? In my mind, an operand is an identifier or name which is aptr.
int b = (*a_ptr++ = 3); is grouped as int b = (*(a_ptr++) = 3);. Note that the parentheses are superfluous; you could have written
int b = *a_ptr++ = 3;
which in many ways makes the result more obvious, since the right-to-left associativity of = is such that the 3 carries over to the value of b.
a_ptr++ is an expression equal to a_ptr but it will point to the second element of the array a once the whole statement completes. Since you don't make use of that incremented pointer, the ++ is a red-herring, so the statement simplifies to
int b = *a_ptr = 3;
whereupon it's clear that *a_ptr = 3 has the effect of setting the first element of the array a to 3 and is an expression equal to 3, which is assigned to b.
The left operand is *a_ptr++. As per the operator precedence, it's evaluated as
*(a_ptr++)
where the post-increment is sequenced as a side effect, after the execution of the statement. The value of the operand is the result of the statement. So, it's equivalent to
int b = (*a_ptr = 3);
a_ptr++;
That said, in general, Operands are expressions or values on which an operator operates or works. So, it can be
a variable (ex: var, as in int var)
a literal (5 or '"Hello"')
an expression (like *a_ptr++)

Why ++(*p) changes the pointer value?

If I have this code:
int A[5] = { 2, 1, 3, 55 };
int *p = A;
cout << ++(*p);
the result is 3 and the value of the first position of A is 3 also, why?
I mean, by hierarchy of operators () is more hierarchical than ++, then we would need operate *p first:
++(*p) => ++(2) => 3
with any change in A vector?
*p is not just "2", it's an lvalue, i.e. this "2" has a well-defined location.
The value at this location is modified by the ++ operator - by definition of the ++ operator.
If you don't want to modify the value, use + 1 instead: *p + 1.
In C/C++, lvalue is a value with a defined location in memory. This value can be changed - by an assignment, incremented, decrement.
For example,
int x = 0;
x = 1; // ok, x is an lvalue, assignment changes the value from 0 to 1
int *p = &x;
*p = 2; // ok, *p is an lvalue, assignment changes the value from 1 to 2
In contrast, an rvalue is a value without a defined location - for example, a result of an arithmetic operation. This value can't be assigned, incremented or decremented (it doesn't mean it can't be used in a larger expression).
For example,
int x = 0, y = 1;
(x + y) = 3; // compilation error, (x + y) is an rvalue
2++; // compilation error, 2 is an rvalue
Here's a pretty simple article explaining lvalues / rvalues: https://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c
The ++(*p) is the same as ++p[0] and ++A[0] All change the first element of the array.
Why ++(*p) changes the pointer value
It does not. The pointer value is value kept in the p. It is called "address" or "reference". It does not change.
It's worth noting the overuse of '*' in C++ which usually confuses novices.
Here:
int *p = A;
'*' means you are declaring p as a variable that can store a memory address (a pointer), and assigning the address of the first position of A (it's the same as &(A[0]) ).
Here:
++(*p)
' *' means you are looking into 'p' content (derreferencing the address of 'p' which is the same as the address of 'A[0]'). Then the '++' increments the value which reflects both.
Just to confirm, after this assignment (int *p = A;) print the values of 'p', 'A' and '&A[0]'.

Issues with assigning result of a function to a variable

#include <iostream>
#include <string>
using namespace std;
int main() {
string userInput;
int stringSize = 0;
userInput = "Hello";
userInput.size() == stringSize;
cout << "Size of userInput: " << stringSize << endl;
return 0;
}
I am trying to get an output of 5, but it is returning 0. I can't figure out what's wrong. The userInput "Hello" should have a size of 5 and userInput.size() should return 5. This should be assigned to variable stringSize. Side note, can anyone explain to me why it is "==" not "="? If I try it with "=", it does not compile and I get this error:
main.cpp: In function ‘int main()’:
main.cpp:11:23: error: lvalue required as left operand of assignment
userInput.size() = stringSize;
^~~~~~~~~~
My understanding is "=" is assignment and "==" is a Boolean comparison that is either "true" or "false" depending on if both sides are equal. I am trying to assign the size of userInput.size() into the variable so I would think that I should use "=". Maybe this is part of the problem?
Here is the answer that was asked for (credits to PacO ):
You always have to put the value that you want to assign to the variable on the right side of the =-operator, and the variable itself to the left side.
What you did, is trying to assign the value of stringSize to userInput.size()
My understanding is "=" is assignment and "==" is a boolean comparison
that is either "true" or "false" depending on if both sides are equal.
Your understanding is correct so far.
I am trying to assign the size of userInput.size() into the variable
so I would think that I should use "="
In general, you cannot write 5 = variable. Left and right in an assignment are not swappable, the order matters.
If you want to assign a value to some variable, the variable must be on the left side of the assignment : variable = 5 .
I used this simplification, but the result of the function size() is like the 5 in my example. This is called a rvalue (like "right-value").
So, in your case, stringSize = userInput.size(); would be correct and set the size of userInput into the variable stringSize (which is called a lvalue a value that can be assigned to, at the left of an assignment).
An lvalue can be seen as a named value that exists past its use, such as a variable. An rvalue doesn't persist past its use, usually the result of a function call or what we'd often refer to as simply a "value".
You can store an rvalue in an lvalue (int i = 2 + 3), but you can't assign an lvalue to an rvalue. That's where your problem exists
The compiler is throwing an error because you have an rvalue userInput.size() and you're treating it as if it's an lvalue by attempting to assign a value to it.
userInput.size() = stringSize
A simpler example would be
int n = 1;
3 = n;
For obvious reasons, you can't just change what 3 is. In your example your function is just returning an integer value...so why should you be able to change that? So you get an error
A different perspective:
You can't set the size of a string. The std::string class automatically calculates the length for you.
So there is no need for the expression:
userInput.size() = stringSize;
To clear the contents of a std::string, use the clear() method:
userInput.clear();
= is an Assignment Operator in C, C++ and other programming languages.
== is assigns the value of right side expression’s or variable’s
value to the left side variable.
Simple example:
int x,y;
x=10;
y=10;
if(x==y)
printf("True");
else
printf("False");
output will be : True
In your case, you have to use one = operator and put function assignment on right of the variable. like this stringSize = userInput.size();
This is the final code:
#include <iostream>
using namespace std;
int main()
{
string userInput;
int stringSize = 0;
userInput = "Hello";
stringSize = userInput.size(); //this line edited.
cout << "Size of userInput: " << stringSize << endl;
return 0;
}
see here online_c++_compiler

Is using an assignment operator in a function argument undefined behaviour?

I found some code similar to this in an example my university tutor wrote.
int main(){
int a=3;
int b=5;
std::vector<int>arr;
arr.push_back(a*=b);
std::cout<<arr[0]<<std::endl;
}
Is there a well-defined behaviour for this? Would arr[0] be 3 or 15 (or something else entirely)?
Visual Studio outputs 15, but I have no idea if that's how other compilers would respond to it.
Before push_back is executed, the expression passed as argument will need to be computed. So what is the value of a *= b. Well it will always be a * b and also the new value of a will be set to that one.
It's valid and works as you expect.
The expression is first evaluated and the result is "returned".
auto& temp = (a*=b);
arr.push_back(temp);
The value of an expression with the compound assignment operator is the value of the left operand after the assignment.
So the code your showed is valid. Moreover in C++ (opposite to C) the result is an lvalue. So you may even write :)
arr.push_back( ++( a *= b ) );
In this case the next statement outputs 16.:)

the increment of a returned value

I have defined a function like this:
int test(int n) {
const int b = n;
return b;
}
While in the main function, I use like this:
int temp = test(50)++;
And the g++ reports an error:
error: lvalue required as increment operand
Actually, I'm fully confused by this. Would you like to give me some tips or explain it to me.
You can only apply ++ to an lvalue (at least of built-in type). The return value from a function can be an lvalue if if it returns a reference, but otherwise it's an rvalue (in which case, you can't apply ++ to it).
The value returned from test is an rvalue. You cannot use the increment operator (++) on it. You can change your calling code to:
int temp = test(50);
temp++;
or
int temp = test(50) + 1;
Once constant value get intialised you cant not change that value.So its giving error try to execute same function without increamenting const value.
'plusplus' operator is (almost) equivalent to '+= 1' that is 'assign the variable its previous value incremented by one'. The value returned is not a variable, so it can not be the left-side argument of an assignment. That's why the increment operator is not applicable here. Just do
t = test(50) + 1;
or
t = test(50);
t ++;