lvalue required as increment operand (2) - c++

Its giving me this error for line *(repetitions + x)++; inside this for loop. Any clues why?
for (int y = 0; y<hours; y++)
{
if (*(array + x) == *(array + y))
{
*(repetitions + x)++;
}
}

You can't increment the rvalue repetitions + x. This is the same error as writing:
int a = 3;
int b = 2;
(a+b)++; // ????
The ++ operator requires an lvalue, i.e. the designation of a variable. a+b is a temporary result and doesn't have a memory address, it can't be incremented.
It's possible that you meant to write (*(repetitions + x))++;, which could be more clearly expressed as repetitions[x]++;

This parses as *((repetitions + x)++) -- that is, it tries to modify a constant address, then dereference it. Presumably you intended increment what the address pointed at instead.
You can do that a couple different ways. One would be to use parentheses. Another to use pre-increment:
++*(repetitions + x);
or:
(*(repetitions + x))++;

Related

Does conditional operator in cpp evaluate both operands?

https://leetcode.com/problems/decode-ways/
my solution:
class Solution {
public:
int numDecodings(string s) {
vector<int> dp(s.size(),0);
for(int i=0;i<s.size();i++)
{
int x =0;
if(s[i]-'0'>0 && s[i]-'0'<=9)
{
x = (i>0)? dp[i-1]:1;
}
if(i!=0 && stoi(s.substr(i-1,2))<=26)
{
cout<<i<<" ";
x = x + (i>=2 )? dp[i-2]:1;
}
dp[i] =x;
}
return dp[s.size()-1];
}
};
Running this code gives this error
Line 924: Char 34: runtime error: addition of unsigned offset to 0x602000000010 overflowed to 0x60200000000c (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_vector.h:933:34
My question is does the conditional operator evaluate dp[i-2] in (i>=2 )? dp[i-2]:1; even if the condition doesn't satisfy? Replacing it with a normal if-else solved my problem.
This line:
x = x + (i>=2) ? dp[i-2] : 1;
is likely not doing what you intend. The ternary ?: has lower precedence than +, so the statement actually becomes:
x = (x + (i>=2)) ? dp[i-2] : 1;
which means you are checking the trueness of x + (i>=2) instead of just i>=2. This is why dp[i-2] can be evaluated even when i < 2, because the entire expression x + (i>=2) could still be true.
You can fix this by putting explicit parentheses yourself:
x = x + ((i>=2) ? dp[i-2] : 1);
or rewriting it like this:
x += i>=2 ? dp[i-2] : 1;
This line doesn't evaluate the way you think it does.
x = x + (i>=2 )? dp[i-2]:1;
Per this page, the addition operator has a higher precedence than the ternary operator. Placing the ternary expression in parentheses should provide the desired behavior.
x += ((i>=2 ) ? dp[i-2] : 1); gets the job done. Even though I changed the operator to remove the redundant x, the parentheses are still necessary.

(C++) Infinite zeros with the addition operator in a small loop

i have a little problem with this small piece of code;
If i use the ++ operation (x++) it works fine, but if i write "x + 1" it breaks and causes an infinite loop of zeros.
The code:
int x = 0;
while (x <= 20)
{
std::cout << x << std::endl;
x + 1;
}
Any help would be appreciated!
If you want to set a value of a variable you must* use an =.
This : x + 1 is NOT the same as x = x + 1 or x += 1 or x++.
When you add that x plus to your code you are never changing the value of x.
x + 1 simply returns a temporary value of the result. Where as the other three listed about will change the value of x instead of creating a temporary value.
You need to know the differences between different statements related to increment as below.
x++ means x = x + 1; which means copy the value of x into temporary
register, increment the value by 1 and write that register value back
into x.
x + 1 means, copy the value of x into temporary register, increment
the value by 1. You are not assigning the value back to x. So x value
won't be changed.
So in your while loop, x value is not being changed which is reason for infinite loop.

What does "expression must be a modifiable lvalue" mean?

int x;
(rand() % 100) + 1 = x;
It keeps saying:
expression should be a modifiable lvalue
Left and right operands of = are not interchangable.
It's the left operand that's being assigned to, so it has to be assignable.
Given int x;, x is assignable (a 'modifiable lvalue'), but x + 42 is not (an rvalue).
It means that you've put the wrong things on each side of the assignment operator (=). The expression on the left should be something you can assign a value to (the "l" in "lvalue" stands for "left"), and the expression on the right should compute the value you want to assign.
Try
x = (rand() % 100) + 1;
instead.
The lvalue always has to be a changeable/modifiable one.
So you can't do :
int a ;
3 = a ;
This means that you are trying to change 3 to a which is not possible because 3 is a constant.
So similarily,
Do this :
x = (rand() % 100) + 1;
And, the error should be gone.

Variable changes on it's own in C++

I have a loop going through an array trying to find which index is a string. It should solve for what that value should be.
I can't figure out why, but as soon as the if statements start i becomes 1 which gives my code an error.
I'm not very fluent in C++.
for(int i = 0; i < 4; i++) {
if(auto value = std::get_if<std::string>(&varArr[i])) {
solvedIndex = i;
auto value0 = std::get_if<float>(&varArr[0]);
auto value1 = std::get_if<float>(&varArr[1]);
auto value2 = std::get_if<float>(&varArr[2]);
auto value3 = std::get_if<float>(&varArr[3]);
//i changes to 1 when this if starts??
if(i = 0) {
solvedVar = (*value3 / *value1) * *value2;
} else if (i = 1) {
solvedVar = *value3 / (*value0 / *value2);
} else if (i = 2) {
solvedVar = *value0 / (*value3 / *value1);
} else {
solvedVar = *value1 * (*value0 / *value2);
}
break;
}
}
Note that these variables are declared above. Also, varArr is filled with values:
std::variant<std::string, float> varArr[4];
int solvedIndex;
float solvedVar;
As has been noted, in your if statements, you are using the assignment operator (=) but want the equality comparison operator (==). For your variable i the first if statement sets i equal to 0 and if(0) is the same as if(false). So your program goes to the first else-if which sets i equal to 1 and if(1) evaluates to true. Your code then finishes the block within else if (i = 1) {...} and then ends.
That's because operator= is the assignment operator in C++ (and most languages, actually). That changes the value of the variable to the value on the other side. So, for instance:
x = 0
will change the value of x to 0. Doesn't matter if it's in an if statement. It will always change the value to 0 (or whatever the right hand side value is).
What you are looking for is operator==, which is the comparison (aka relational) operator in C++/ That asks the question "Are these two things equal?" So, for instance:
x == 0
asks is x is equal to 0.

Validity of *(ptra + 0).prop and (*(ptra + 0)).prop

Why is *(ptra + 0).prop not valid and (*(ptra + 0)).prop valid ? . I know that left side of the dot operator must have a structure. But I am still confused. Could someone explain to me the difference between the two ?
class myobj
{
public:
int v;
};
int main()
{
myobj *ptra = new myobj[2]();
*(ptra + 0).v = 12 //Error
(*(ptra + 0)).v = 12 ; //OK
return 0;
}
Because operator . has a higher precedence then operator * , so
*(ptra + 0).v
means
*((ptr + 0).v)
and not
(*(ptra + 0)).v
*((ptr + 0).v) here is incorrect syntax, because (ptr + 0) is not a class or union, so has no any members.
Errors like this one are due to operator precedence. In this case, the first operator that gets executed is operator. and the second that gets executed ins operator*. This is why one line compiles and the other doesn't, even if they look very much alike.
To override this operator precedence, you use parenthesis, as in
(1 + 2) * 3 = 9
as opposed to
1 + 2 * 3 = 7.