|
|
1.1 root 1:
2:
3: cast Definition cast
4:
5:
6:
7:
8: The cast operation ``coerces'' a variable from one data type to
9: another.
10:
11: There are two reasons to cast a variable. The first is to con-
12: vert a variable's data into a form acceptable to a given func-
13: tion. For example, the function hhyyppoott takes two ddoouubbllees. If the
14: variables lleegg_xx and lleegg_yy are ffllooaatts, the rules of C require that
15: they be cast automatically to ddoouubbllee. If the compiler did not do
16: not do this, hhyyppoott would grab a ddoouubbllee's worth of memory: the
17: four bytes of your ffllooaatt, plus four bytes of whatever happens to
18: be sitting on the stack. The leads to results that are less than
19: totally accurate.
20:
21: The other reason to cast a variable is when you cast one type of
22: pointer to another. For example,
23:
24:
25: char *foo;
26: int *bar;
27: bar = (int *)foo;
28:
29:
30: Although ffoooo and bbaarr are of the same length, you would cast foo
31: in this instance to stop the C compiler from complaining about a
32: type mismatch.
33:
34: ***** See Also *****
35:
36: data formats, data types, definitions
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.