goodman goodman - 10 months ago 66
C Question

Multiple unsequenced modifications warning not applied to members of struct

I have found a curious exception to a multiple unsequenced modifications warning. I have come across the warning when looping a counter:

int i = 0;
i = ++i & 0xFF; // <- warning

warning: multiple unsequenced modifications to 'i'

I understand why this is undefined behaviour. What I have found though is that this warning isn't applied when using struct members:

typedef struct
int i;

struct_t t;
t.i = ++t.i & 0xFF; // <- no warning

The same applies for pointers as well:

struct_t *u;
u = &t;
u->i = ++u->i & 0xFF; // <- no warning

I am using MDK-ARM Plus version 5.21a in C99 mode.

My question is whether this is actually acceptable code, that for some reason because I am using structs it is no longer undefined behaviour, or is it that the compiler warnings are just missing it.

Answer Source

Your two examples are identical as far as sequence points are concerned. Both of them invokes undefined behavior (as per 6.5), since i is modified twice in the same expression with no sequence point in between. The data type of i doesn't matter, nor does it matter if it is part of a struct.

It is not acceptable code. However, the compiler isn't required to give you a diagnostic for code that invokes undefined behavior. If you do get one, then that's awfully nice of the compiler.

The reason why your compiler is inconsistent in its warnings appears to be a minor compiler bug.