1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Defining Typedef Names (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Defining Typedef Names (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Defining Typedef Names (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="index.html" rel="up" title="Top">
|
---|
33 | <link href="Statements.html" rel="next" title="Statements">
|
---|
34 | <link href="Enumeration-Types.html" rel="prev" title="Enumeration Types">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Defining-Typedef-Names"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Statements.html" accesskey="n" rel="next">Statements</a>, Previous: <a href="Enumeration-Types.html" accesskey="p" rel="prev">Enumeration Types</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Defining-Typedef-Names-1"></span><h2 class="chapter">18 Defining Typedef Names</h2>
|
---|
64 | <span id="index-typedef-names"></span>
|
---|
65 | <span id="index-typedef"></span>
|
---|
66 |
|
---|
67 | <p>You can define a data type keyword as an alias for any type, and then
|
---|
68 | use the alias syntactically like a built-in type keyword such as
|
---|
69 | <code>int</code>. You do this using <code>typedef</code>, so these aliases are
|
---|
70 | also called <em>typedef names</em>.
|
---|
71 | </p>
|
---|
72 | <p><code>typedef</code> is followed by text that looks just like a variable
|
---|
73 | declaration, but instead of declaring variables it defines data type
|
---|
74 | keywords.
|
---|
75 | </p>
|
---|
76 | <p>Here’s how to define <code>fooptr</code> as a typedef alias for the type
|
---|
77 | <code>struct foo *</code>, then declare <code>x</code> and <code>y</code> as variables
|
---|
78 | with that type:
|
---|
79 | </p>
|
---|
80 | <div class="example">
|
---|
81 | <pre class="example">typedef struct foo *fooptr;
|
---|
82 |
|
---|
83 | fooptr x, y;
|
---|
84 | </pre></div>
|
---|
85 |
|
---|
86 | <p>That declaration is equivalent to the following one:
|
---|
87 | </p>
|
---|
88 | <div class="example">
|
---|
89 | <pre class="example">struct foo *x, *y;
|
---|
90 | </pre></div>
|
---|
91 |
|
---|
92 | <p>You can define a typedef alias for any type. For instance, this makes
|
---|
93 | <code>frobcount</code> an alias for type <code>int</code>:
|
---|
94 | </p>
|
---|
95 | <div class="example">
|
---|
96 | <pre class="example">typedef int frobcount;
|
---|
97 | </pre></div>
|
---|
98 |
|
---|
99 | <p>This doesn’t define a new type distinct from <code>int</code>. Rather,
|
---|
100 | <code>frobcount</code> is another name for the type <code>int</code>. Once the
|
---|
101 | variable is declared, it makes no difference which name the
|
---|
102 | declaration used.
|
---|
103 | </p>
|
---|
104 | <p>There is a syntactic difference, however, between <code>frobcount</code> and
|
---|
105 | <code>int</code>: A typedef name cannot be used with
|
---|
106 | <code>signed</code>, <code>unsigned</code>, <code>long</code> or <code>short</code>. It has
|
---|
107 | to specify the type all by itself. So you can’t write this:
|
---|
108 | </p>
|
---|
109 | <div class="example">
|
---|
110 | <pre class="example">unsigned frobcount f1; /* <span class="roman">Error!</span> */
|
---|
111 | </pre></div>
|
---|
112 |
|
---|
113 | <p>But you can write this:
|
---|
114 | </p>
|
---|
115 | <div class="example">
|
---|
116 | <pre class="example">typedef unsigned int unsigned_frobcount;
|
---|
117 |
|
---|
118 | unsigned_frobcount f1;
|
---|
119 | </pre></div>
|
---|
120 |
|
---|
121 | <p>In other words, a typedef name is not an alias for <em>a keyword</em>
|
---|
122 | such as <code>int</code>. It stands for a <em>type</em>, and that could be
|
---|
123 | the type <code>int</code>.
|
---|
124 | </p>
|
---|
125 | <p>Typedef names are in the same namespace as functions and variables, so
|
---|
126 | you can’t use the same name for a typedef and a function, or a typedef
|
---|
127 | and a variable. When a typedef is declared inside a code block, it is
|
---|
128 | in scope only in that block.
|
---|
129 | </p>
|
---|
130 | <p><strong>Warning:</strong> Avoid defining typedef names that end in ‘<samp>_t</samp>’,
|
---|
131 | because many of these have standard meanings.
|
---|
132 | </p>
|
---|
133 | <p>You can redefine a typedef name to the exact same type as its first
|
---|
134 | definition, but you cannot redefine a typedef name to a
|
---|
135 | different type, even if the two types are compatible. For example, this
|
---|
136 | is valid:
|
---|
137 | </p>
|
---|
138 | <div class="example">
|
---|
139 | <pre class="example">typedef int frobcount;
|
---|
140 | typedef int frotzcount;
|
---|
141 | typedef frotzcount frobcount;
|
---|
142 | typedef frobcount frotzcount;
|
---|
143 | </pre></div>
|
---|
144 |
|
---|
145 | <p>because each typedef name is always defined with the same type
|
---|
146 | (<code>int</code>), but this is not valid:
|
---|
147 | </p>
|
---|
148 | <div class="example">
|
---|
149 | <pre class="example">enum foo {f1, f2, f3};
|
---|
150 | typedef enum foo frobcount;
|
---|
151 | typedef int frobcount;
|
---|
152 | </pre></div>
|
---|
153 |
|
---|
154 | <p>Even though the type <code>enum foo</code> is compatible with <code>int</code>,
|
---|
155 | they are not the <em>same</em> type.
|
---|
156 | </p>
|
---|
157 | <hr>
|
---|
158 | <div class="header">
|
---|
159 | <p>
|
---|
160 | Next: <a href="Statements.html" accesskey="n" rel="next">Statements</a>, Previous: <a href="Enumeration-Types.html" accesskey="p" rel="prev">Enumeration Types</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
161 | </div>
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 | </body>
|
---|
166 | </html>
|
---|