This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 8 years ago.
I have done some searching, but didn't find the answer
The code:
char b = 'b';
char c = 'c';
char a[5] = "";
a[0] = b, c;
What last line means? The b, c part?
Thank you all
That uses the elusive comma operator to cause confusion.
It evaluates b, and result of that is then asssigned to a[0]. After that, c is evaluated but its value thrown away. At least this is the case in C.
The comma has lower precedence than assignment (see this handy table) which is extra confusing.
Related
This question already has answers here:
double '=' in initialization
(3 answers)
Closed 3 years ago.
My apologies if this is a duplicate: searching for this isn't easy.
Example code taken from rtorrent:
m_bindings[KEY_UP] = m_bindings['P' - '#'] = std::bind(&ElementDownloadList::receive_prev, this);
What does the double value-setting mean, and how can this statement be explained?
The expression is evaluated from the right equals sign to the left. The statement a = b = c can be rewritten a = (b = c). The result of an = operation is the value that was assigned. Thus the result of (b = c) is c, making the next operation equivalent to a = c.
This is similar to x = y = 1 which is short hand for y = 1 and x = y.
This is called operator chaining. What you are doing is assigning the return value of the right hand operator = to the left hand operator =
It is equivalent to doing
m_bindings['P' - '#'] = std::bind(&ElementDownloadList::receive_prev, this);
m_bindings[KEY_UP] = m_bindings['P' - '#'];
but saves you a line of code. It also saves you from calling operator[] a second time, which could be expensive. Personally, I would use the 2 line version to make the code easier to read unless performance is really an issue.
This question already has answers here:
Why does i[arr] work as well as arr[i] in C with larger data types?
(5 answers)
Closed 7 years ago.
The following code
#include<stdio.h>
int main()
{
int arr[] = {10,20,30};
cout << -2[arr];
return 0;
}
prints -30. How? Why?
In your case,
cout<<-2[arr];
gets translated as
cout<<-(arr[2]);
because,
array indexing boils down to pointer arithmatic, so, the position of array name and the index value can be interchanged in notation.
The linked answer is in C, but valid for C++ also.
regarding the explicit ()s, you can check about the operator precedence here.
Look at this statement
cout << -2[arr];
First, know that even though it looks strange the following is true
2[arr] == arr[2]
That being said operator[] has higher precedence than -. So you are actually trying to invoke
-(arr[2])
In C and C++, 2[arr] is actually the same thing as arr[2].
Due to operator precedence, -2[arr] as parsed as -(2[arr]). This means that the entire expression evaluates to negating the 3rd element of arr, i.e. -30.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I have here an equation i can't understand how c++ process this. Can someone explain this operation?
code:
#include <stdio.h>
main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i = 0;
int num = a[i+++a[++i]]+a[++i+i++];
printf("\nnum1: %d i: %d,num,i);
}
why is the answer num = 9 while index i is just equal to 4;
Using ++ twice in the same expression on the same variable is explicitly undefined by all versions of both the C and C++ standards, and so i does not necessarily equal 4. It could be anything at the whim of the compiler writer.
Never do this. Never use ++ and -- twice in the same expression. There is no way to make any statement about what the resultant value will be, and no experience with what it does with one compiler will mean anything with respect to what another compiler does.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 9 years ago.
Example code
int arr[3] = { 0, 1 };
int* buf = arr;
*buf++ = *buf + 10;
Result of last expression is that buf[0] == 10. I taught it would be buf[0] == 11.
A college of mine wrote something similar to the example code and I taught it works differently than it does. And I would like to know why it works the way it does.
The way I went about figuring it out was to look at the operator precedence table. There it states that suffix ++ has precedence over dereference. Hence I taught that on the left of operator= buf would point to the first element, but on the right of the operator= it would have already been incremented and would point to the second element. However that is not the case.
My question is, why is that so? Preferably a standard quote :) However any explanation is welcome!
You are accessing and modifying the pointer multiple times in a single sequence point. This is undefined behavior.
More generally, reading from and writing to any variable between sequence points is undefined. The fact that you have a pointer in this specific example is by-the-by.
To avoid confusion with the pointer:
int i = 0;
i++ = i + 1; // UB
Logically, should the i on the right hand side be the "current" value of i, or the modified value? This is why it is undefined. Instead, separate the code:
int i = 0;
++i;
i = i + 1;
Which is clear, and well defined.
This question already has answers here:
C extension: <? and >? operators
(2 answers)
Closed 9 years ago.
I saw this code
a<?=b; // (a and b are int)
from the solution of Google Code Jam.
but my VS shows an error on '?'
I only know the following:
a>b?a=0:b=0;
Thanks.
Old operator; it's a (since removed) gcc extension for 'minimum'. That is:
a <?= b;
is the same as:
a = a < b ? a : b;
A nonstandard GCC extension to C++ allows <? as an operator which is equivalent to min. I haven't seen <?= before, but presumably it's an in-place version; that is, a <?= b is equivalent to a = min(a,b).
Note that the GCC developers woke up the next morning and realized what a bad idea that was. The operator is now deprecated.
If a happens to be larger than b, it would set a to b.
Essentially the same as:
a = a < b ? a : b;
Example:
int a = 5;
int b = 2;
a<?=b; //a is now 2!
I wouldn't advocate actually using such a solution though, it is horrible.
As others have said, it's part of a nonstandard GCC extension, but please don't use it.