Pre-increment operators when using a variable on the same line

I believe that what I am trying to do is probably fair, because it is separated in both instances of a comma (not a typical task), but I have no idea and the search does not cause anything about these two specific situations.

In both cases, I used the variable as an index for two parallel arrays.

int a[3] = {10, 20, 30};
int b[3] = {20, 40, 60};

Case # 1: Structure Initialization for Arrays

struct testStruct {
     int t1;
     int t2;
};
int i = 0;
testStruct test = {a[++i], b[i]}

The expected result of the final line: test = {20, 40}

Situation # 2: Passing specific values ​​from arrays as args functions

void testFunc(int t1, int t2) {
    // do stuff
}
int i = 0;
test(a[++i], b[i]);

The expected result of the final line: test(20, 40)

Is this valid code? And if so, is this true in all compilers?

Is the result what I expect? If so, is it because of arrays or because of a comma?

Thank!

+4
2

"" , , . , , :

testStruct test = {a[++i], b[i]}

:

++i ;
testStruct test = {a[i], b[i]}

, , , .

, , ++ 11 pre ++ 11.

, pre ++ 11 . , ++ 11 , 430 :

GCC (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11633)

int count = 23;   int foo[] = { count++, count++, count++ };

undefined - ?

( ):

, , (1.9 [intro.execution]/12-13; . 392), (1.9 [intro.execution]/16). , , , , , , . - , ?

++ 11 ++ 11 8.4.5 , : >

- -, (14.5.3), , . , , , , , .

, ++ 11 , , , .

undefined , . undefined 15 1.9, :

, , . [ . , , . -end note] . , , undefined.

:

f(i = -1, i = -1); // the behavior is undefined
+4

testStruct test = {a[++i], b[i]};

( ), ++

4 - -, (14.5.3), , . , , , , , .

, GCC 4.8.1 (, , ) ,

, , , , goofle "translate"

test(a[++i], b[i]);

undefined, .

test( { a[++i], b[i] } );

.

+2

Source: https://habr.com/ru/post/1545361/


All Articles