packed byte array in struct doubt

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
Bump. (Last and only, honest!)
(shrugging shoulders)...

I'm only familiar with GCC's __attribute__(packed, aligned(1)) which does what you are saying. Can't speak for other compilers though.
In microsoft, you can use:
1
2
3
4
#pragma pack(push,1)
//structures here

#pragma pack(pop) 

Thanks for the replies. I am aware of these compiler-specific extensions. Unfortunately, the code might be expected to compile and operate with some unknown compiler on some really weird hardware.

My question is really just, does a byte array of a single byte always only occupy a single byte?
I am not 100% sure but if I would guess the padding occurs at the end of the struct since the compiler wants to optimize the whole struct so you should be home free. It would seem weird to pad in the middle of the struct cause this cause compability issues like when you interact with 3rd party libraries. But this is just a hunch, haven't done any research.
I'm thinking that since everything in the struct is, essentially, just a single byte, that there shouldn't be any padding issues.

Thanks all for the interest. For now I will just continue assuming that is correct. (It wouldn't be too much to change things if it isn't, I guess...) Heh. Weird stuff.
Topic archived. No new replies allowed.