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.
Related
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.
#include<iostream>
int main()
{
int a = 5;
a = (a = 10, a++, a--);
std::cout << a;
}
Output is 11,
But when I modify the line
a = a=10,a++,a--;
Output is 10
What effect does removing the ( ) operator has and in what order the operators are being executed.
The + and - operators take precedence before the assignment operator and the +,- signs are read by the compiler from left to right, whereas the assignment operators are read by the compiler from right to left. So:
1) a++
2) a--
3) a=10
4) a =
The c++ compiler will pass your complete set code to a binary tree which will rearrange your code according to the precedence of the operator = + - etc in left node or right node
I'm studying C++ as a beginner and my book explain me what is assignment but i can't understand a little concept that the book is trying to make me understand:
int a = 3; // a starts out with the value 3
a = a + 7; // a gets the value of a + 7 (that is, 10)
What my book says is: the last assignment deserves notice.First of all it clearly shows that = doesn't mean equals-clearly, a doesn't equal to a + 7.It means assignment, that is, to place a new value in a variable. What does it mean ? how should i read an assignment?
For example:
a = a + 7; // should i read it as: a is equal to a + 7 ?
The code might be expanded in following way:
int a; // declaration of the variable. a has undefined value.
a = 3; // Assign the value 3 to a. a is 3.
a = a + 7; // Assign value of (a + 7) to a. a is 10.
The expression (a + 7) is evaluated first and the value of (3 + 7) is assigned to a.
The token = is used for assignment and doesn't have anything to do with equality.
The assignment means that the right-hand side (rhs) will be evaluated to a single value, then assigned to the left-hand side (lhs)
a = a + 7;
Now, rhs --> a + 7 --> 3 + 7 --> 10, now we have a single value, and will assign 10 to a
In C++ = is the assignment operator. == is the equality operator.
Read the chapter about operator precence (the order in which operators performs operations on its operands).
= (the assignment operator) is a binary operator, has lowest precedence (that is the reason first a+7 operation is performed) and it assigns the result of an expression on its right side to the variable on its left side.
For, a = a + 7, never read it as a equals a + 7, but make habit of reading it as a gets assigned the value of a + 7 (or whatever the expression) is.
The = also replaces the exiting value of variable on left side (a) with the result of expression on right side ( a + 7).
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))++;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ Comma Operator
Uses of C comma operator
I am not new to C++, but this is the first time I see the following code:
int a=0;
int b=(a=2,a+1);
That is C++ code. Can you tell me what is going on here? And how variable b gets value 3?
This code is equivalent to this:
int a = 2 ;
int b = a + 1 ;
First expression to the left of comma gets evaluated, then the one to its right. The result of the right most expression is stored in the variable to the left of = sign.
Look up the comma operator for more details.
http://en.wikipedia.org/wiki/Comma_operator
(a = 2, a + 1); return 3 because in general case operator (a, b) return b, and calculation in (a, b) started from right to left. So, in your case, (a = 2, a + 1) return a + 1, and after operator a = 2 was executed a + 1 return 3.