[alt.unix.wizards] Packed C-structures in Interactive UNIX

jga@jt.dk (Jens Groth Andreasen) (12/10/90)

I'm developing an application for a PC running INTERACTIVE UNIX System V/386 
Release 3.2 using the standard C compiler ('cc').
For reasons beyond my control, some of the structures I use must be 
byte aligned (i.e. no padding with unused bytes). I was hoping that
'#pragma pack(1)' could do the job for me, but as the following example show
the directive doesn't work 100% correctly.

#pragma pack(1)

typedef struct {
     char e1;
     int e2;
} S1;

typedef struct {
     char e1;
     int e2;
     char e3;
} S2;

typedef struct {
     S1 s1;
     S2 s2;
} S3;

static S3 s3 = {
	'c', 4711,
	'q', 117, 'w'

printf("%c %i\n%c %i %c\n", s3.s1.e1, s3.s1.e2,
			    s3.s2.e1, s3.s2.e2, s3.s2.e3 );

When I compile this with 'cc example.c', the output of the program will be: 
g 1962934290
 1902313472 w

If I remove the #pragma pack(1), the output will be the expected, but if I
now compile the (modified) program with 'cc example.c -Zp1' ( the -Zp[1|2|4]
option will byte-, two-byte- or word-align members of structures resp.) I get 
the strange output from before.

Can anyone running on a similar system recreate this?
Can anyone explain this?

If I initialize the structure by assignments (like 's3.s1.e2 = 4711') I get
the right output (with or without the #pragma pack(1)).

Please mail me even if you respond by posting, I'll post a summary of the 

Regards, Jens.

email: jga@jt.dk
       jgroth@daimi.aau.dk  (this address is the most reliable one)

FAX: +45 86 29 39 77
Phone: +45 86 29 33 66 ext. 6925  (open 7.15 - 15.15 GMT)

Letter post: Jens Groth Andreasen
	     Jutland Telephone
	     Sletvej 30
	     DK-8310 Tranbjerg J.
	     Denmark (Europe)

danabu@scorpio.uucp (Daniel N. Abushanab) (12/14/90)

I'm very new to shell programming, so maybe this is a stupid question,
but I'll ask away anyway.  I'm trying to write a script that will look
in a directory, see how many files are in there, then randomly select
one to be displayed as a background picture.  Currently I'm using the
time as a selection criterion, where a different picture is displayed
depending on what time it is when the request is made.  Not very
elegant, but like I said, I'm new at this.  Any help will be greatly
appreciated.  Thanks.

Daniel N. Abushanab		          University of California, Berkeley
E-mail:  danabu@scorpio.berkeley.edu      Mechanical Engineering Department
phone:  (415) 642-5109