

cast                        Definition                       cast




The cast  operation ``coerces'' a variable from  one data type to
another.

There are two  reasons to cast a variable.  The  first is to con-
vert a  variable's data into  a form acceptable to  a given func-
tion.  For example, the function hhyyppoott takes two ddoouubbllees.  If the
variables lleegg_xx and lleegg_yy are ffllooaatts, the rules of C require that
they be cast automatically to ddoouubbllee.  If the compiler did not do
not do  this, hhyyppoott  would grab a  ddoouubbllee's worth of  memory: the
four bytes of your ffllooaatt,  plus four bytes of whatever happens to
be sitting on the stack.  The leads to results that are less than
totally accurate.

The other reason to cast a  variable is when you cast one type of
pointer to another.  For example,


        char *foo;
        int *bar;
        bar = (int *)foo;


Although ffoooo and  bbaarr are of the same length,  you would cast foo
in this instance to stop  the C compiler from complaining about a
type mismatch.

***** See Also *****

data formats, data types, definitions



























COHERENT Lexicon                                           Page 1


