+ 2

Why is a[1]=1?

In the following code why is a[1]=1?: int x=0; int a[]={3,4}; a[x]=++×; cout<<a[1]; Shouldn't a[1] be 4?

19th Oct 2016, 5:53 PM
Doru Catalin Dregoesc
Doru Catalin Dregoesc - avatar
6 Answers
+ 4
++x is a pre-increment. That is done before the statement is executed. Therefore you are setting a[1] to 1.
19th Oct 2016, 6:00 PM
Cohen Creber
Cohen Creber - avatar
+ 1
int x=0; ++x=1 so a[x]=a[0] becomes a[1] since ++x=1. ++x is pre-increment, hence the value is increased before we solve the expression.
1st Nov 2016, 8:38 PM
Ant Bear
Ant Bear - avatar
0
3
19th Oct 2016, 6:00 PM
malathi
0
yes me too I can not understand
21st Oct 2016, 11:11 AM
samira
samira - avatar
0
++ has bigger priority from = and [] , so x=1 and you did a [1]=1
28th Dec 2016, 11:37 PM
dimitris kuriakopoulos
dimitris kuriakopoulos - avatar
0
=1
12th Jan 2017, 5:34 AM
Aaron
Aaron - avatar