Increment Operator in arrays C++ -


this question has answer here:

the following code gives output : ccb

int t = 0; char a[] = {'a', 'b', 'c'}; cout<<a[t]<<a[++t]<<a[++t]; 

i want know, happening in code generates specified output. appreciated.

my initial reaction: c++ not guarantee order of evaluation on statement

cout<<a[t]<<a[++t]<<a[++t]; 

you need along lines of:

cout<<a[t]<<a[t + 1]<<a[t + 2]; t += 3; 

upon further review, in response r sahu's comments (thanks): order of evaluation may determinate because operator << function call , function call sequence point. order not expected. in case t++ operations executed in right-to-left order because arguments each function call must evaluated before call made. i.e. statement actually:

cout ( << a[t] ( << a[++t] ( << a[++t] ) ) ) ; 

and innermost phrase evaluated first.

of course above parenthesized expression not valid. technically correct should say:

operator << (operator << ( operator << ( cout, a[++t]), a[++t]), a[t]); 

but confusing because in order write expression in form have reverse order of index operators (which compiler does).

and in response baum's comments: decomposing function calls way not make call determinate because order in function arguments evaluated not specified evaluate operator << (cout, a[++t]) before evaluating a[t]

the solution, avoiding operations side-effects in complex statements, still valid.


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -