Does casting lvalue lead to Undefined Behaviour ?



Please see the code below

-- start listing is_it_ub.c --

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
unsigned char buff[20];
unsigned int i;

i = 0xaabbccddUL;
*((int *)buff) = i; /* Is this UB ? */

printf("buff: %x:%x:%x:%x\n", buff[0], buff[1], buff[2], buff[3]);

return EXIT_SUCCESS;
}

-- end listing --

Output:
buff: dd:cc:bb:aa

Output seems correct on my Little Endian PC.

In the statement: "*((int *)buff) = i; ", buff is casted to be treated
as an int *. Is this valid?

My compiler does not produce any diagnostics for the above
program but somebody else complained that their compiler
warns "casting of lvalue is deprecated".

Thanks.

.



Relevant Pages