byte alignment in structures and unions

R

Richard

CBFalconer said:
pete said:
Army1987 said:
anon.asdf wrote:

I want to assign the number 129 (binary 10000001)
to the MSB (most significant byte) of a 4-byte long
and leave the other lower bytes in-tact!
Try
((unsigned char *)&val)[3] = 129;
No unions and no struct needed.

You can assign the value of 129
to the highest addressed byte of any object,
this way:

((unsigned char *)&val)[sizeof val - 1] = 129;

Seems to give funny results on my machine. (Which happens to place
the MSByte of an integer in the lowest order address). The world
is not defined by an X86.

But in the context of this question it is.

What is your platform independent solution?

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top