Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site spp3.UUCP Path: utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!ittatc!dcdwest!sdcsvax!sdcrdcf!trwrb!trwspp!spp3!ansok From: ansok@spp3.UUCP (Gary Ansok) Newsgroups: net.lang.c Subject: Re: type cast in initializer Message-ID: <269@spp3.UUCP> Date: Fri, 7-Feb-86 18:15:55 EST Article-I.D.: spp3.269 Posted: Fri Feb 7 18:15:55 1986 Date-Received: Tue, 11-Feb-86 04:27:22 EST References: <302@hsi.UUCP> <1450@bbncc5.UUCP> <305@cray.UUCP> Organization: TRW, Redondo Beach CA Lines: 23 > int x = 0; > char *ptr = (char *) &x; Even if the pointers (int *) and (char *) have different formats, the assignment should take care of the conversion, so the cast should be unnecessary. A cast in an assignment var_1 = (type) var_2 is never strictly necessary, except for readability (a good reason!), to keep lint happy, or to work around bad compilers. (The cast *should* be acceptable to the compiler, though). As I understand it, a cast has the effect of an assignment to a temporary unnamed variable; sqrt((double) i) is equivalent to (tmp_double = i, sqrt(tmp_double)). Are there any other cases where casts are NEEDED besides: function calls: doub_var = sqrt((double) int_var); pointer punning: long_var = *(long *) char_ptr; Note: that last example is NONportable and downright dangerous on machines with alignment requirements. Still, it gets used by a lot of programmers (including me, on occasion...sigh). Gary Ansok {ihnp4,ucbvax,decvax}!trwrb!trwspp!spp3!ansok