Take the following:
int a(void) {
puts("a");
return 0;
}
int b(void) {
puts("b");
return 1;
}
int c(void) {
puts("c");
return 2;
}
int d(void) {
puts("d");
return 3;
}
Will the following have predictable behavior?
int arr[4][4][4][4];
arr[a()][b()][c()][d()] = 1;
Is it guaranteed to print in this order:
a
b
c
d
I am aware that constructs such as the following are invalid:
int i;
i = i++;
This is because =
is an unsequenced operator, so whether i
or i++
is evaluated first is undefined. It is undefined behavior to access and modify a single object before another sequence point.
Put another way, is the following valid:
int i = 0, arr[4][4][4][4];
arr[i++][i++][i++][i++] = 1;
Or does it invoke undefined behavior due to unsequenced modification and access to i
?
According to the C standard, is there a defined sequence point between each successive []
while indexing a multidimensional array?
To be clear, neither of these examples have to do with precedence, the placement of implicit parenthesis, the order in which the operators operate on their operands. The question is about sequencing, the order in which the operands themselves are evaluated.
x[i]
is defined as(*((x)+(i)))
, sox[i][j]
becomes(*((x[i])+(j)))
which becomes(*(((*((x)+(i))))+(j)))
. There are no sequence points in that expression, so the order of evaluation is unspecified. (If you remove extraneous parentheses, it simplifies to*(*(x+i)+j)
.)