k = x[j] +++j;
is the same as
k = (x[j]++) +j;
Also, k = x[j] + ++j
is undefined behavior, because the evaluation order of addition is not defined. Apart from the fact that the compiler is allowed to produce anything, both of these two snippets are equally sound interpretations of that line:
j++;
k = x[j] + j;
and
j++;
k = x[j-1] + j;
In general, avoid being "smart". If you're not 100% sure what you're doing, don't use ++
and --
in more complex statements. Do the increment or decrement on a separate line before or after. That will save you tons of trouble. For instance, can you without any doubt and without googling it say exactly what
while(*str++) {
/* Do something */
}
means? IMHO it's clearer with
while(*str) {
str++;
/* Do something */
}
Sure it costs a line, and reducing the number of lines can be a good thing, but in this case I think it's better to play it safe. In this particular case you can also make it clearer with parenthesis: *(str++)
if you prefer, but I think you see my point.
I actually made an answer where this was the actual problem: https://stackoverflow.com/a/56522806/6699433