1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>The
<cw>sizeof<
/> operator
</title>
10 <link href=
"sx9c.html" rev=precedes
>
11 <link href=
"sx9e.html" rel=precedes
>
12 <link href=
"sx9.html" rev=subdocument
>
15 <H2>The
<cw>sizeof<
/> operator
</H2>
19 may seem like an excessively roundabout or low-level way
20 of finding the number of elements in an array,
21 but it is the way it's done in C,
22 and it's perfectly safe and straightforward
23 once you get used to it.
24 (I would, however, be hard-pressed to defend against the
25 accusation that it's a bit too low-level.)
26 </p><p>Note that
<TT>sizeof
</TT> works on both type names
27 (things like
<TT>int
</TT>,
<TT>char *
</TT>,
<TT>struct key
</TT>, etc.)
29 (strictly speaking, any expression).
30 Parentheses are required
31 when you're using
<TT>sizeof
</TT> with a type name
33 when you're using it with a variable or expression
34 (just like
<TT>return
</TT>),
35 but it's safe to just always use parentheses.
36 </p><p><TT>sizeof
</TT> returns the size counted in
<dfn>bytes
</dfn>,
37 where the C definition of ``byte'' is ``the size of a
<TT>char
</TT>.''
38 In other words,
<TT>sizeof(char)
</TT> is always
1.
39 (It turns out that it's not necessarily the case,
41 that a byte or a
<TT>char
</TT> is
8 bits.)
42 When we start doing our own dynamic memory allocation
43 (which will be pretty soon),
44 we'll always be needing to know the size of things
45 so that we can allocate space for them,
47 that we're meeting and getting used to the
<TT>sizeof
</TT> operator now.
49 ``But the expression in the
<TT>#define
</TT>
50 is not evaluated by the preprocessor''
52 as far as the preprocessor is concerned,
53 the ``value'' of the macro
<TT>NKEYS
</TT>
54 (like the value of any macro)
55 is just a string of characters like
56 <pre> (sizeof(keytab) / sizeof keytab[
0])
57 </pre>which it replaces wherever
<TT>NKEYS
</TT> is used,
58 and which will then be evaluated by the compiler as usual,
60 that the preprocessor wouldn't have known
61 how to deal with the
<TT>sizeof
</TT> operator,
62 or how big the
<TT>keytab
</TT> array or a
<TT>struct key
</TT> were.
63 </p><p>A third way of defining
<TT>NKEYS
</TT> would be
64 <pre> #define NKEYS (sizeof(keytab) / sizeof *keytab)
65 </pre></p><p>Note that
66 the definition of
<TT>NKEYS
</TT>
68 the definition of the
<TT>keytab
</TT> array
69 (which appears on page
133),
72 will have to precede the use of
<TT>NKEYS
</TT>
73 in
<TT>main
</TT> on page
134.
74 (Also, all three will have to be in the same source file,
75 unless other steps are taken.)
77 </p><p>Notice that
<TT>getword
</TT> has a lot in common with the
78 <TT>getop
</TT> function of the calculator example
79 (section
4.3, page
80).
83 <a href=
"sx9c.html" rev=precedes
>prev
</a>
84 <a href=
"sx9e.html" rel=precedes
>next
</a>
85 <a href=
"sx9.html" rev=subdocument
>up
</a>
86 <a href=
"top.html">top
</a>
89 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
90 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
91 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>