1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 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>18.1.6 Type Definitions (
<TT>typedef
</TT>)
</title>
10 <link href=
"sx4ea.html" rev=precedes
>
11 <link href=
"sx4ga.html" rel=precedes
>
12 <link href=
"sx4a.html" rev=subdocument
>
15 <H3>18.1.6 Type Definitions (
<TT>typedef
</TT>)
</H3>
17 <p>[This section corresponds to K
&R Sec.
6.7]
18 </p><p>When the storage class is
<TT>typedef
</TT>,
23 a declaration means something completely different than it usually does.
24 Instead of declaring a variable named
<TT>count
</TT>,
25 we are declaring a
<em>new type
</em> named
<TT>count
</TT>.
26 (Actually, we're just declaring a new name for an old type;
27 you can think of typedef names as type aliases.)
28 Having declared this new type
<TT>count
</TT>,
29 we can now use it as a base type in other declarations,
32 count napples, noranges;
34 </p><p>The types
<TT>FILE
</TT> and
<TT>size_t
</TT> which we've seen at
35 various points along the way
36 (which we've described as being ``new types defined by the
37 header file
<TT><stdio.h
></TT>''
38 [or also several other headers in the case of
<TT>size_t
</TT>])
41 both defined using
<TT>typedef
</TT>.
42 </p><p>You can use
<TT>typedef
</TT> to define new names for complicated
47 typedef struct listnode list, *nodeptr;
49 after which you can declare several strings (
<TT>char *
</TT>'s)
52 string string1, string2;
54 or several lists (
<TT>struct listnode
</TT>)
59 or several pointers to list nodes by saying
63 </p><p><TT>typedef
</TT> provides a way to simplify structure declarations.
64 In a previous section,
65 we saw that we had to declare
66 new variables of type
<TT>struct complex
</TT>
69 struct complex c1, c2;
71 Using
<TT>typedef
</TT>, however,
72 we can introduce a single-word complex type, after all:
74 typedef struct complex complextype;
77 It's also possible to define
78 a structure and a typedef tag for it at the same time:
80 typedef struct complex
87 when using typedef names,
88 you may not need the structure tag at all;
97 (At this point, of course,
98 you culd use the cleaner name ``
<TT>complex
</TT>''
99 for the typedef, instead of ``
<TT>complextype
</TT>''.
100 Actually, it turns out that you could have done this all along.
101 Structure tags and typedef names share separate namespaces,
104 typedef struct complex
111 though possibly confusing.)
112 </p><p>Defining new type names is done mostly for convenience, or to
113 make the code more self-documenting, or to make it possible to
114 change the actual base type used for a lot of variables without
115 rewriting the declarations of all those variables.
116 </p><p>A
<TT>typedef
</TT> declaration is a little bit like a
117 preprocessor
<TT>#define
</TT> directive.
118 We could imagine writing
121 #define string char *
123 in an attempt to accomplish the same thing.
124 This won't work nearly as well, however:
125 given the macro definition,
128 string string1, string2;
132 char * string1, string2;
134 which would declare
<TT>string1
</TT> as a
<TT>char *
</TT>
135 but
<TT>string2
</TT> as a plain
<TT>char
</TT>.
136 The typedef declaration,
138 would work correctly.
139 </p><p>Some programmers capitalize typedef names to make them stand out
141 and others use the convention of ending all typedef names with
142 the characters ``
<TT>_t
</TT>''.
146 <a href=
"sx4ea.html" rev=precedes
>prev
</a>
147 <a href=
"sx4ga.html" rel=precedes
>next
</a>
148 <a href=
"sx4a.html" rev=subdocument
>up
</a>
149 <a href=
"top.html">top
</a>
152 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
153 //
<a href=
"copyright.html">Copyright
</a> 1996-
1999
154 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>