Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: how to write () a constant? Keywords: how to, write(), constant Message-ID: <1990Jan8.113957.3778@virtech.uucp> Date: 8 Jan 90 11:39:57 GMT References: <10883@attctc.Dallas.TX.US> Distribution: na Organization: Virtual Technologies Inc. Lines: 29 In article <10883@attctc.Dallas.TX.US>, bobc@attctc.Dallas.TX.US (Bob Calbridge) writes: > A quick question if you please, Suppose you want to write a binary constant > to a file. Would the following code be valid? > > write(handle, (int *) '0x01', sizeof(int)) No. What this code would do is either core dump with a memory fault or write out the sizeof(int) bytes starting at location 0x30783031 or location 0 depending upon how the compiler interpreted '0x01' but anyway, the value at this location would probably not be a 1. If you wanted to write out a 1, you would have to do something like: write(handle (char *) &0x01, sizeof(int)) But this is illegal since you cannot use & on a constant. So what you have to do is: intvar = 0x01; write(handle, (char *) &intvar, sizeof(int)) -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+