Little problem with sizeof on PC
Mark Allender
allender at ux1.cso.uiuc.edu
Tue Apr 23 12:20:57 AEST 1991
I'm having a litle problem that I have a suspicision about, but want
to clarify. Here's the situation....
I have a structure that is defined like:
struct header {
int version[2];
char unused[40];
int stuff[8];
char bogus;
char mode;
int time;
char unused2[90];
char filler[38];
char filler2[15]
float number;
};
The total size of the structure is 201 bytes (count it if you wish....).
Now, I want to read the beginning of a binary file into this structure,
so I do something like this:
struct header Header;
if ((readnum = read(fd, (char *)(&Header), sizeof(Header)).....
Things don't seem to get done correctly at this point. A little investigation
shows that sizeof(Header) return 202, and not 201. This is clearly not
what I want to do.
Now, I kind of figure that the problem has to do with the way structure
members are lined up in memory. Am I correct in thinking that since there
are an odd number of bytes in the structure (201), that sizeof(Header)
returns an even number since things have to be word aligned (with a word
being 2 bytes)? This would seem to make some sense.
In any case, what is the best way around this problem. Could I do something
like
if ((readnum = read(fd, (char *)(&Header), sizeof(Header) - 1))....
Seems like kind of a bad way to fix things....
Any help would be appreciated...
-Thanks in advance...
-Mark Allender
-University of Illinois at Urbana/Champaign
-Conversation Builder Project
-allender at cs.uiuc.edu
More information about the Comp.lang.c
mailing list