Re: union to get parts of integer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



ratheesh k <ratheesh.ksz@xxxxxxxxx> writes:

> typedef struct {
> char parts[4];
> } node ;
>
> int i=0x12345678
>
> ((node *)&i)->parts[0];
> ((node *)&i)->parts[1];
> ((node *)&i)->parts[2];
> ((node *)&i)->parts[3];
>
> Is there any mechanism to split into bytes using the power of union ?

The above should work (assuming of course that sizeof(int) == 4).  The
following should work as well:

((char *)&i)[0].

However, are you sure that you need this?  Don't you need "(i & 255)",
"((i >> 8) & 255)", etc. instead?

-- 
Best regards,                                         _     _
 .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
 ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
 ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--

Attachment: pgpz69YsLRU_z.pgp
Description: PGP signature


[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux