Hi. Here comes another esoteric, oddball question. ;-]
I need to parse byte-packed data from a stream in C89. I would like to use one-byte byte arrays in the structure just for the convenience of getting the address without the visual overhead. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#define BYTE( name ) byte name[ 1 ]
#define WORD( name ) byte name[ 1 ], JOIN( x1_, __LINE__ )
#define DWORD( name ) byte name[ 1 ], JOIN( x1_, __LINE__ ), JOIN( x2_, __LINE__ ), JOIN( x3_, __LINE__ )
typedef struct {
BYTE( one );
DWORD( two );
...
} Fooey_t;
dword get_dword( byte* src ) { ... }
/* extracts the network-byte-ordered, probably-
* unaligned datum into a machine-local integer type
*/
|
What this convenience entails is that casting the type onto the bytestream automatically produces a dereferencible address:
1 2 3
|
byte* bytes;
Fooey_t* fooey = (Fooey_t*)bytes;
dword twos_value = get_dword( fooey->two );
|
instead of having to use an explicit address-of operator (and the not-required but visually-explicit parentheses):
1 2 3 4
|
#define WORD( name ) byte name, JOIN( x1_, __LINE__ )
...
byte* bytes;
dword twos_value = get_dword( &(fooey->two) );
|
I know this is horribly convoluted, *sigh*, but I cannot make any assumptions about the hardware on which my code will operate... and I simply won't do the unreadable, parse-everything-directly-from-the-byte-array-to-begin-with method. (I want
readable code that is easy to manipulate --not a massive list of bit-shifting operations over an amorphous byte array.)
I figure the question is at best academic, but it is still worth the asking:
Does a single-element byte array get byte-packed into a structure just like a single byte?
Or is there some C89 compiler out there that will pad it out to some machine word size?
(Remembering that no structure contains any data type other than byte-sized elements.)
Didn't I say it was an abstruse question? Thanks for spending your neurons here.
:@ XD