type cast in initializer
rex ballard
rb at ccivax.UUCP
Wed Feb 12 13:37:56 AEST 1986
In article <269 at spp3.UUCP> ansok at spp3.UUCP (Gary Ansok) writes:
>
>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;
>
Yes, accessing members of structures.
ie:
struct x y;
struct i j;
i.memb=y.memb; /* some compilers hate this */
should be written:
i.memb= ((struct i)x).memb;
this is especially true if both structures contain 'memb' but
'memb' is different type or placement.
A union is probably preferred.
More information about the Comp.lang.c
mailing list