I am a little confused about how bytes are ordered in a struct .
Let's say I have the following structure:
struct container { int myint; short myshort; long mylong; };
Now I want to initialize a variable of type struct container in the same way as the next, except that I want to do this with an array.
struct container container1 = {.myint = 0x12345678, .myshort = 0xABCD, .mylong = 0x12345678};
Assume sizeof int and long 4 , and << 28> - 2 .
Assume no indentation.
How then would a 10 bytes struct layout be?
Does it depend on continent?
It would be like this:
0x12345678 ABCD 12345678
or how:
0x78563412 CDAB 78563412
What I want to do: I have the following char array:
char buffer[10] = {0};
I want to manually populate this array with data, and then memcpy on a struct .
Should I do [1] :
buffer[0] = 0x12345678 & 0xFF; buffer[1] = 0x12345678 >> 8 & 0xFF; buffer[2] = 0x12345678 >> 16 & 0xFF; buffer[3] = 0x12345678 >> 24 & 0xFF; ... buffer[9] = 0x12345678 >> 24 & 0xFF;
or should be [2] :
buffer[0] = 0x12345678 >> 24 & 0xFF; buffer[1] = 0x12345678 >> 16 & 0xFF; buffer[2] = 0x12345678 >> 8 & 0xFF; buffer[3] = 0x12345678 & 0xFF; ... buffer[9] = 0x12345678 & 0xFF;
before i do my memcpy like:
memcpy(&container1, buffer, sizeof(container1);
And if I write to an array and copy it to a struct , is it transported across systems, especially regarding the statement?
EDIT : Does [1] work on a small end machine and [2] on a large endian?