Does casting lvalue lead to Undefined Behaviour ?
- From: p_cricket_guy@xxxxxxxxxxx
- Date: 28 Dec 2006 21:48:23 -0800
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.
.
- Follow-Ups:
- Re: Does casting lvalue lead to Undefined Behaviour ?
- From: Walter Roberson
- Re: Does casting lvalue lead to Undefined Behaviour ?
- From: Richard Heathfield
- Re: Does casting lvalue lead to Undefined Behaviour ?
- From: Harald van Dijk
- Re: Does casting lvalue lead to Undefined Behaviour ?
- Prev by Date: Re: one's complement of unsigned char
- Next by Date: Re: Segment
- Previous by thread: one's complement of unsigned char
- Next by thread: Re: Does casting lvalue lead to Undefined Behaviour ?
- Index(es):
Relevant Pages
|