All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Reza Hoorfar" <rhoorfar@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: RE: union to get parts of integer
Date: Mon, 20 Dec 2010 15:33:06 +0330	[thread overview]
Message-ID: <000601cba03d$e10b1440$a3213cc0$@gmail.com> (raw)
In-Reply-To: <AANLkTikAB_-kO_cmD5VQRtztx=4V9NDcmU4uWBDE=ON8@mail.gmail.com>

union MyUnion{
	int myInt;
	char myChar[sizeof(int)];
};

void main()
{
	MyUnion m;
	m.myInt = 0x01020304;
	printf("value :%d\n",m.myChar[0]); // -> value : 4
	printf("value :%d\n",m.myChar[1]); // -> value : 3
	printf("value :%d\n",m.myChar[2]); // -> value : 2
	printf("value :%d\n",m.myChar[3]); // -> value : 1
}

-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of ratheesh k
Sent: Monday, December 20, 2010 2:10 PM
To: linux-c-programming@vger.kernel.org
Subject: union to get parts of integer

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 ?

-Ratheesh
--
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


  reply	other threads:[~2010-12-20 12:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-20 10:40 union to get parts of integer ratheesh k
2010-12-20 12:03 ` Reza Hoorfar [this message]
2010-12-20 14:44 ` Michal Nazarewicz
2010-12-20 15:30   ` Tim Walberg
2010-12-20 15:40     ` Michal Nazarewicz
2010-12-21 16:22     ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='000601cba03d$e10b1440$a3213cc0$@gmail.com' \
    --to=rhoorfar@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.