[Icc-avr] Re: Casting (Morten Dramstad)

David Brown david_brown at hotpop.com
Mon Jan 14 15:29:40 PST 2008


Morten Dramstad wrote:
> Hi!
> I always cast like this:
> 
> 
> 
> *(int *)&data[7] = channelOne;
> 
> data[7] will then be equal to the LSB and data[8] the MSB ofchannelOne.
> 
> The same for a long variable:
> 
> char data[10];
> long longVar;
> 
> *(long *)&data[4] = longVar;
> 
> data[4] will be set equal to the LSB of longVar
> data[5] will be set equal to the second LSB of longVar
> data[6] will be set equal to the second MSB of longVar
> data[7] will be set equal to the MSB of longVar
> 
> This will compilate into the shortest possible assembly code(is my
> experience using different compilers, including Imagecraft ICCAVR).
> 

Just be careful to remember that this solution is non-portable - if you 
ever step up to a 16-bit or 32-bit cpu, you could get serious problems 
because of alignment issues.




More information about the Icc-avr mailing list