Farouk Sabry
Farouk Sabry

Reputation: 165

error: unsequenced modification and access to <argument>

In this code here:

#include <stdio.h>

#define SHIFT(value,num_of_bits) ((num_of_bits) > 0) ? \
                ((value) <<= (num_of_bits)) : ((value) >>= (-(num_of_bits)))

int main(void)
{
    unsigned int w1 = 0177777u, w2 = 0444u;

    printf("SHIFT(w1,5) = %o\tw1 << 5 = %o\n", SHIFT(w1,5), w1 << 5);

    return 0;
}

Here when I use clang to compile, I get the following error:

error: unsequenced modification and access to w1

But when I compile it using gcc, no errors are generated.

I saw this question here unsequenced modification and access to pointer but the answers didn't help actually. So I don't get it, why this is happening ? and what does that error mean ?

Upvotes: 1

Views: 724

Answers (1)

Simon Woo
Simon Woo

Reputation: 484

That's because you are trying to modify the first argument value in the definition of SHIFT. Try to modify the macro as follows:

#define SHIFT(value,num_of_bits) ((num_of_bits) > 0) ? \
                ((value) << (num_of_bits)) : ((value) >> (-(num_of_bits)))

Upvotes: 2

Related Questions