how to write () a constant?

Conor P. Cahill cpcahil at virtech.uucp
Mon Jan 8 22:39:57 AEST 1990


In article <10883 at attctc.Dallas.TX.US>, bobc at 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     |
+-----------------------------------------------------------------------+



More information about the Comp.lang.c mailing list