GCC Error: lvalue required as left operand of assignment - c

I have a char* pointer and when I do (++pointer)= NULL, there's an error:
lvalue required as left operand of assignment
What does it mean and how do I fix it? The code is
void func(const char*p1, const char *p2){
char * pointer;
pointer=malloc(strlen(p1)+strlen(p2)+2;
(++pointer)=NULL;
}

The symbol pointer is an lvalue and can be used in an assignment on the left hand side. (++pointer), however, is not an lvalue and cannot be used in the same assignment.
If on an alien planet far far away it did compile, this code would increment pointer by one and then set it to zero (NULL) so to achieve the same effect, and be portable to earth compilers, use pointer = NULL;

The subexpression (++pointer) will evaluate to an rvalue and it can't be a left operand of assignment (=) operator.

Some expressions cannot be an assignable l-value.
In your code (++pointer) is an expression, which cannot be used as a left operand of the = sign.
Think of something like
(x + 2) = 5 // (x + 2) is an expression
Does it makes sense?

Related

Why It throws an error lvalue is required?

#include<stdio.h>
int main(){
int i=0,j=1;
printf("%d",++(i+j));
return 0;
}
In this code I have used the increment operator but i don't know why it throws an error.
It throws the error:
lvalue is required.
You are trying to increment an integer value that isn't assigned to a variable.
Taking your code, this is approximately what the computer will try to do:
printf("%d",++(i+j));
// expanded step 1
printf("%d",++(0+1));
// expanded step 2
printf("%d",++(1));
As you can see in the last version, you are trying to call ++1, which is invalid.
In order to increment a value using ++, the operand must have integral, floating, or pointer type and must be a modifiable l-value expression (an expression without the const attribute):
int x = i+j;
printf("%d",++x);
Alternatively, you can use the addition operator:
printf("%d",i+j+1);
You cant increment the result.
Instead:
printf("%d",i + j + 1);
In C you cannot apply an unary operator too a number. When the value between the parentesis is evaluated first... before use the unary operation. assing the result to a variable.
#include<stdio.h>
int main(){
int i=0,j=1, other_value=0;
other_value = i+j;
printf("%d",++(other_value));
return 0;
}
Your previus code will work will get the error
error: lvalue required as increment operand
An "lvalue" is a value that can be the target of an assignment. The "l" stands for "left", as in the left hand side of the equals sign. An rvalue is the right hand value and produces a value, and cannot be assigned to directly. If you are getting "lvalue required" you have an expression that produces an rvalue when an lvalue is required.
in your case when compiler manipulate the expression then it will result to invalid expression which is ++1
try below:
printf("%d",i+j+1);
The expression ++(i+j) is not assignable. Here, the expression (i+j) will get evaluated first. For the ++ operator, the operand must have integral, floating, or pointer type and must be a modifiable l-value expression. Since the result from i+j is a constant value, so you get the error lvalue required as increment operand. You can choose to modify this expression as i+j+1.

Why can't you increment/decrement a variable twice in the same expression?

When I try to compile this code
int main() {
int i = 0;
++(++i);
}
I get this error message.
test.c:3:5: error: lvalue required as increment operand
++(++i);
^
What is the error message saying? Is this something that gets picked up by the parser, or is it only discovered during semantic analysis?
++i will give an rvalue1 after the evaluation and you can't apply ++ on an rvalue.
§6.5.3.1 (p1):
The operand of the prefix increment or decrement operator shall have atomic, qualified, or unqualified real or pointer type, and shall be a modifiable lvalue.
1. What is sometimes called "rvalue" is in this International Standard described as the "value of an expression". - §6.3.2.1 footnote 64).
A lvalue is a value you can write to / assign to.
You can apply ++ to i (i is modified) but you cannot apply ++ to the result of the previous ++ operator. I wouldn't have any effect anyway.
Aside: C++ allows that (probably because ++ operator returns a non-const reference on the modified value)
The issue that the (++i) returns new integer value, and please note ++ operation needs some variable for assignment, not a value (you are trying to increment an integer not a variable), so you can use this instead :
i += 2;
or
i = i + 2;

lvalue required as unary ‘&’ operand -- passing function result as pointer

I have some problem with my code.
There are the following functions:
static Poly PolyFromCoeff(int coeff);
static Mono MonoFromPoly(const Poly *p, int exp);
And in another function I have this line:
Mono m = MonoFromPoly(&PolyFromCoeff(10),4);
But I receive this error message:
lvalue required as unary ‘&’ operand
If I save the first result to a variable, there is no error:
Poly p = PolyFromCoeff(10);
Mono m = MonoFromPoly(&p,4);
Why is the first solution wrong?
As it says, operator & requires a lvalue as its argument, i.e. it cannot be applied to temporary values. Addresses are not associated with values, with objects only.
In the second form you instantiate an object that holds this value and you can easily take the address of that object.
The C language expressly prohibits you from taking the address of a rvalue (which is what a function returns). This clause from the C11 standard (committee draft) sums it up:
6.5.3.2 Address and indirection operators
Constraints
The operand of the unary & operator shall be either a function designator, the result of a
[] or unary * operator, or an lvalue that designates an object that is not a bit-field and is
not declared with the register storage-class specifier.
If you are confused about lvalue and rvalue, think of it like this:
lvalue is something that has an identifier and storage
rvalue is a temporary result or literal value
If you have a C++ background, you might have been confused because the behavior of references is different. In C++, it's okay to have this:
static Poly PolyFromCoeff(int coeff);
static Mono MonoFromPoly(const Poly &p, int exp);
Mono m = MonoFromPoly( PolyFromCoeff(10), 4 );

Using Increment operator with de-referencing in C

To my function i get a void pointer, I would like to point to the next location considering the incoming pointer is of char type.
int doSomething( void * somePtr )
{
((char*)somePtr)++; // Gives Compilation error
}
I get the following compilation error:
Error[Pe137]: expression must be a modifiable lvalue
Is this an issue with the priority of operators?
A cast does not yield an lvalue (see section 6.5.4 footnote 104 of C11 standard), therefore you can't apply post increment ++ operator to its result.
c-faq: 4.5:
In C, a cast operator does not mean "pretend these bits have a different type, and treat them accordingly"; it is a conversion operator, and by definition it yields an rvalue, which cannot be assigned to, or incremented with ++. (It is either an accident or a deliberate but nonstandard extension if a particular compiler accepts expressions such as the above.)
Try this instead
char *charPtr = ((char*)somePtr);
charPtr++;
If you want to move the pointer to next then you can use:
*ptr++;
If you want to Change copy the pointer position to another variable then:
char *abc = (char*)(def + 1);
It really depends on your motive to do things

int *p=&a++ and int *p=&++a

The code is simple,I just want to test the priority between & and ++.
int main()
{
int a=10;
int *p;
int *q;
p=&++a;//error: lvalue required as unary ‘&’ operand
q=&a++;//error: lvalue required as unary ‘&’ operand
return 0;
}
when gcc compile it, two errors came out. In my opinion,a++ is a right value,but the ++a is a lvalue,so it should not be an error when use & to get it address,
It seems that you have misunderstood the difference between lvalue and rvalue. In your case both the expressions a++ and ++a are rvalues and cannot be assigned.
Here is a good description of the distinction, but the quick way to tell whether it is an lvalue is to see whether you get an error if you put it on the left side of an =.
Observe that both of the following two statements produce the same error you describe.
++a = 5;
a++ = 6;
Update: As #mafso mentioned below, ++a is indeed an lvalue in the language c++, and you will not see the error for that expression when compiling with g++.
the object of operator & is lvalue , an the same is operator ++.but after ++a is done , it returns rvalue , that's the reason wrong.

Resources