Is the structure address the same as its first element address?
Yes, it is actually provided by the C and C ++ standards. From standard C:
6.7.2.1-13. A pointer to a structure object, appropriately transformed, points to its initial member
The size of your struct should be two bytes. You should not convert the pointer to it to char* : instead, you should use memcpy to copy your Bitmask to the buffer that you send over the network.
EDIT . Since you use I / O with scatter distribution with iovec , you do not need to throw Bitmask at anything: iov_base is void* , so you can just set iov[0].iov_base = header;
Note. This only works as long as your struct does not contain virtual functions, base classes, etc. (thanks Timo).
EDIT2
To get {0x81, 0x05} in your struct , you must reorder the elements of the structure as follows:
struct Bitmask { unsigned char opcode: 4; unsigned char rsv3: 1; unsigned char rsv2: 1; unsigned char rsv1: 1; unsigned char fin: 1; unsigned char payload_length: 7; unsigned char mask: 1; }
source share