In general, don't use sub-expressions with side-effects. This includes =, ++, --, *=, +=, etc. Specifically, one should avoid constructs like:

	*++p = 0;
	c = a[i+=2];
	x = y += 3;
	bar( c++ );
	if( (f = foo()) != 0 ) ...

Note that only sub-expressions with side-effects are to be avoided; its correct to write:

	p += 2;

Operands of the comma operator should be treated as top-level expressions for this purpose. So its acceptable to write:

	for( i = 0, j = 0; i < N; i++, j *= 2 )

There are a couple of reasons to avoid sub-expressions with side-effects should be avoided. First it is easy to make a mistake; the difference between correct code and code that may or may not work is very slim. For instance:

	for( i = 0, j = 0; i < N; i++ ) {
	  a[j++] = i;

is correct; however the somewhat similar code below is not:

	for( j = 0; j < N; ) {
	  a[j++] = j;

I have found that these mistakes don't usually happen when you write the code the first time. They are more frequent when you, or someone else, later modifies the code, either to fix a bug or to add more function.

What makes these kinds of bugs even more dangerous is that the code may work as intended. However, simply by changing the optimization level, or even by adding other code to the function, the compiler may now output code that behaves differently.

The other reason to avoid sub-expressions with side-effects are that they are difficult to understand. For instance, what does *++p = (*q)++; mean? Even if you can understand it immediately, the person who comes in later to maintain/modify the code may not.

Next Prev Main Top Feedback