* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes.int / sx4fa.html
blobb222648ea6bd14a49d5dc87f58c6f04695043ced
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
15 <H3>18.1.6 Type Definitions (<TT>typedef</TT>)</H3>
17 <p>[This section corresponds to K&amp;R Sec. 6.7]
18 </p><p>When the storage class is <TT>typedef</TT>,
19 as in
20 <pre>
21 typedef int count;
22 </pre>
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,
30 such as
31 <pre>
32 count napples, noranges;
33 </pre>
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>&lt;stdio.h&gt;</TT>''
38 [or also several other headers in the case of <TT>size_t</TT>])
39 are
40 typically
41 both defined using <TT>typedef</TT>.
42 </p><p>You can use <TT>typedef</TT> to define new names for complicated
43 types, too.
44 You could define
45 <pre>
46 typedef char *string;
47 typedef struct listnode list, *nodeptr;
48 </pre>
49 after which you can declare several strings (<TT>char *</TT>'s)
50 by saying
51 <pre>
52 string string1, string2;
53 </pre>
54 or several lists (<TT>struct listnode</TT>)
55 by saying
56 <pre>
57 list list1, list2;
58 </pre>
59 or several pointers to list nodes by saying
60 <pre>
61 nodeptr np1, np2;
62 </pre>
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>
67 using the syntax
68 <pre>
69 struct complex c1, c2;
70 </pre>
71 Using <TT>typedef</TT>, however,
72 we can introduce a single-word complex type, after all:
73 <pre>
74 typedef struct complex complextype;
75 complextype c1, c2;
76 </pre>
77 It's also possible to define
78 a structure and a typedef tag for it at the same time:
79 <pre>
80 typedef struct complex
82 double real;
83 double imag;
84 } complextype;
85 </pre>
86 Furthermore,
87 when using typedef names,
88 you may not need the structure tag at all;
89 you can also write
90 <pre>
91 typedef struct
93 double real;
94 double imag;
95 } complextype;
96 </pre>
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,
102 so the declaration
103 <pre>
104 typedef struct complex
106 double real;
107 double imag;
108 } complex;
109 </pre>
110 is legal,
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
119 <pre>
120 #define count int
121 #define string char *
122 </pre>
123 in an attempt to accomplish the same thing.
124 This won't work nearly as well, however:
125 given the macro definition,
126 the line
127 <pre>
128 string string1, string2;
129 </pre>
130 would expand to
131 <pre>
132 char * string1, string2;
133 </pre>
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,
137 however,
138 would work correctly.
139 </p><p>Some programmers capitalize typedef names to make them stand out
140 a little better,
141 and others use the convention of ending all typedef names with
142 the characters ``<TT>_t</TT>''.
143 </p><hr>
145 Read sequentially:
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>
150 </p>
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>
155 </p>
156 </body>
157 </html>