Re: FAQ Related - why cast?
From: Martin (martin.o_brien_at_[no-spam)
Date: 01/05/05
- Next message: freeday: "a c project for help!"
- Previous message: Jonathan Burd: "Re: Newbie question"
- In reply to: Dietmar Schindler: "Re: FAQ Related - why cast?"
- Next in thread: Eric Sosman: "Re: FAQ Related - why cast?"
- Reply: Eric Sosman: "Re: FAQ Related - why cast?"
- Reply: CBFalconer: "Re: FAQ Related - why cast?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 05 Jan 2005 17:13:34 GMT
"Dietmar Schindler" <dSpam@arcor.de> wrote in message
news:41DBBCB6.52BA@arcor.de...
> Provided that you stated the FAQ answer correctly, the explanation is
> nonsense (the left hand side of the assignment expression is of type
> int, and without the cast, the right hand side is also of type int; so
> there is no extension).
To ensure the partial quote I gave in my initial post was not misleading,
this is the question and answer from the book (c)1996 by Addison-Wesley
Publishing Company, Inc.
Question: How can I write code to conform to these old, binary data file
formats?
Answer: It's difficult because of word size and byte-order differences,
floating-point formats, and structure padding. To get the control you need
over these particulars, you may have to read and write things a byte at a
time, shuffling and rearranging as you go. (This isn't always as bad as it
sounds and gives you both code portability and complete
control.) For example, suppose that you want to read a data structure,
consisting of a character, a 32-bit integer, and a 16-bit integer, from the
stream fp into the C structure
struct mystruct {
char c;
long int i32;
int i16;
};
You might use code like this:
s.c = getc(fp);
s.i32 = (long)getc(fp) << 24;
s.i32 |= (long)getc(fp) << 16;
s.i32 |= (unsigned)(getc(fp) << 8);
s.i32 |= getc(fp);
s.i16 = getc(fp) << 8;
s.i16 |= getc(fp);
This code assumes that getc reads 8-bit characters and that the data is
stored most significant byte first ("big endian"). The casts to (long)
ensure that the 16- and 24-bit shifts operate on long values (see question
3.14), and the cast to (unsigned) guards against sign extension. (In
general, it's safer to use all unsigned types when writing code like this,
but see question 3.19.)
The corresponding code to write the structure might look like:
putc(s.c, fp);
putc((unsigned)((s.i32 >> 24) & 0xff), fp);
putc((unsigned)((s.i32 >> 16) & 0xff), fp);
putc((unsigned)((s.i32 >> 8) & 0xff), fp);
putc((unsigned)(s.i32 & 0xff), fp);
putc(s.i16 >> 8) & 0xff, fp);
putc(s.i16 & 0xff, fp);
See also questions 2.12, 12.38, 16.7, and 20.5.
-- Martin http://martinobrien.co.uk/
- Next message: freeday: "a c project for help!"
- Previous message: Jonathan Burd: "Re: Newbie question"
- In reply to: Dietmar Schindler: "Re: FAQ Related - why cast?"
- Next in thread: Eric Sosman: "Re: FAQ Related - why cast?"
- Reply: Eric Sosman: "Re: FAQ Related - why cast?"
- Reply: CBFalconer: "Re: FAQ Related - why cast?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|