By casting buf to an int you have instructed the compiler NOT to treat
it as a byte.
It is beholden on you the software engineer to ensure whatever you
cast is compatible with what you cast it too. In this case you are
saying that &buf[i] is word aligned, which will be only true for one
in four cases of i. If the compiler aligns the start of buf to a word
bondary which will happen under some cases &buf[0] will be the only
legal cast.
FordP
--- In lpc2000@yahoogroups.com, David Hawkins <dwh@...> wrote:
>
> dsidlauskas1 wrote:
> > Consider the following code:
> >
> > ============================
> > char buf[]={1,2,3,4,5,6,7,8};
> > int *ip, x[4];
> >
> > for (i=0; i<4; i++)
> > {
> > ip = (int *)&buf[i];
> > x[4] = *ip;
> > }
>
> Er, given the fact that x is of length 4,
> the statement x[4] = *ip is actually out-of-bounds.
>
> Dave
>Message
Re: For C Experts
2006-03-30 by fordp2002
Attachments
- No local attachments were found for this message.