Possible Duplicate:
C++ Comma Operator
I am initializing an array with
int main()
{
int arr[3]= { (1,3), 2, 4 };
cout << arr[0] << " " << arr[1] << " " << arr[2] << endl;
}
I thought it would give a compile time error but it is running fine. The array is initialized with values 3,2,4 and output is also 3 2 4.
Can someone explain what is happening here ?
You’re looking at the comma operator there. Basically, the expression:
will evaluate
1but “return” 7.That particular form you have (as well as mine above) is not that useful but you can do things like:
to both increment
aand setxto 1 (the usefulness comes from side effects).You will have seen this before without necessarily realising it: