source: public/doc/gnu-c/Defining-Typedef-Names.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 6.5 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
68use 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
70also called <em>typedef names</em>.
71</p>
72<p><code>typedef</code> is followed by text that looks just like a variable
73declaration, but instead of declaring variables it defines data type
74keywords.
75</p>
76<p>Here&rsquo;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
78with that type:
79</p>
80<div class="example">
81<pre class="example">typedef struct foo *fooptr;
82
83fooptr 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&rsquo;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
101variable is declared, it makes no difference which name the
102declaration 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
107to specify the type all by itself. So you can&rsquo;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
118unsigned_frobcount f1;
119</pre></div>
120
121<p>In other words, a typedef name is not an alias for <em>a keyword</em>
122such as <code>int</code>. It stands for a <em>type</em>, and that could be
123the type <code>int</code>.
124</p>
125<p>Typedef names are in the same namespace as functions and variables, so
126you can&rsquo;t use the same name for a typedef and a function, or a typedef
127and a variable. When a typedef is declared inside a code block, it is
128in scope only in that block.
129</p>
130<p><strong>Warning:</strong> Avoid defining typedef names that end in &lsquo;<samp>_t</samp>&rsquo;,
131because many of these have standard meanings.
132</p>
133<p>You can redefine a typedef name to the exact same type as its first
134definition, but you cannot redefine a typedef name to a
135different type, even if the two types are compatible. For example, this
136is valid:
137</p>
138<div class="example">
139<pre class="example">typedef int frobcount;
140typedef int frotzcount;
141typedef frotzcount frobcount;
142typedef 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};
150typedef enum foo frobcount;
151typedef int frobcount;
152</pre></div>
153
154<p>Even though the type <code>enum foo</code> is compatible with <code>int</code>,
155they are not the <em>same</em> type.
156</p>
157<hr>
158<div class="header">
159<p>
160Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.