From: j DOT aldrich6 AT genie DOT com Message-Id: <199607220024.AA286965065@relay1.geis.com> Date: Mon, 22 Jul 96 00:03:00 UTC 0000 To: dclayton AT dircon DOT co DOT uk Cc: djgpp AT delorie DOT com Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii Subject: Re:Float to integer conversion Reply to message 0521777 from DCLAYTON AT DIRC on 07/21/96 5:39PM >Could somebody please tell me the best way to change an integer number to a >float, and vice versa? Depends on what you need it for. Here's a sample program which demonstrates some basic (and not so basic) concepts of typecasting: #include #include /* defines double sqrt( double n ); */ struct foo { char name[20]; long socsec; char dummy; }; int main( void ) { int x = 10; float y = 5.55; struct foo bar; char *bytes; x = (int) y; y = (float) x; y = (float) sqrt( (double) x ); printf( " x = %d\n (float) x = %f\n (char) x = %c\n (long double *) x = %p\n", x, (float) x, (char) x, (long double *) x ); strcpy( bar.name, "John Adams" ); bar.socsec = 123456789; bar.dummy = '\0'; bytes = (char *) &bar; printf( "%s\n", bytes ); printf( "The social security number is %ld\n", ((struct foo *) bytes)->socsec ); bytes = (char *) malloc( 16 * 1024 * 1024 * sizeof *bytes ); printf( "Address of allocated memory = %p\n", bytes ); return 0; } WARNING: Anything more complex than char-int-float typecasting should be examined very carefully to make sure it won't damage anything! For example, if I had not created a 'dummy' field in the above example and set it to zero, the "printf( bytes );" line would have done some very strange things. See if you can figure out why. :) John