Little problem with sizeof on PC

Robert Earl rearl at gnu.ai.mit.edu
Tue Apr 23 18:02:30 AEST 1991


In article <1991Apr23.050747.19705 at agate.berkeley.edu> c60b-1eq at e260-1d.berkeley.edu (Noam Mendelson) writes:

|   In article <1991Apr23.022057.29511 at ux1.cso.uiuc.edu> allender at ux1.cso.uiuc.edu (Mark Allender) writes:
|
|   >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)).....
|
|   Hmmm.  That should be:
|      if ((readnum = read(fd, (struct header *)(&Header), sizeof(Header)) ...

Nope, (char *) &Header is right, because "&Header" is already a
"struct header *" and read() expects a "char *" argument.

|   The best thing would be to restructure the data file, using 202 byte blocks
|   instead of 201 to be on the safe side.  At worst, you're increasing the
|   size of the data file by about .5%.

If it were just a data file, the best thing would be to make it a
plain text file and use fscanf() or fgets(), but he appears to want
only the header bit of it, so he should just be very careful to note
this binary representation could break under another operating
system/CPU/phase of the moon.

--robert



More information about the Comp.lang.c mailing list