<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh">
	<id>https://arolstar52-zhtest.hf.space/index.php?action=history&amp;feed=atom&amp;title=Typedef</id>
	<title>Typedef - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://arolstar52-zhtest.hf.space/index.php?action=history&amp;feed=atom&amp;title=Typedef"/>
	<link rel="alternate" type="text/html" href="https://arolstar52-zhtest.hf.space/index.php?title=Typedef&amp;action=history"/>
	<updated>2026-07-03T12:16:06Z</updated>
	<subtitle>在这个wiki上该页的修订历史</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://arolstar52-zhtest.hf.space/index.php?title=Typedef&amp;diff=233353&amp;oldid=prev</id>
		<title>imported&gt;Sakurase：​回退163.19.6.3（对话）的编辑，改回Terry850324的最后一个版本</title>
		<link rel="alternate" type="text/html" href="https://arolstar52-zhtest.hf.space/index.php?title=Typedef&amp;diff=233353&amp;oldid=prev"/>
		<updated>2025-02-24T06:36:40Z</updated>

		<summary type="html">&lt;p&gt;回退&lt;a href=&quot;/wiki/Special:%E7%94%A8%E6%88%B7%E8%B4%A1%E7%8C%AE/163.19.6.3&quot; title=&quot;Special:用户贡献/163.19.6.3&quot;&gt;163.19.6.3&lt;/a&gt;（&lt;a href=&quot;/index.php?title=User_talk:163.19.6.3&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;User talk:163.19.6.3（页面不存在）&quot;&gt;对话&lt;/a&gt;）的编辑，改回&lt;a href=&quot;/wiki/Special:%E7%94%A8%E6%88%B7%E8%B4%A1%E7%8C%AE/Terry850324&quot; title=&quot;Special:用户贡献/Terry850324&quot;&gt;Terry850324&lt;/a&gt;的最后一个版本&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{noteTA&lt;br /&gt;
|G1=IT&lt;br /&gt;
|1=zh-cn:声明; zh-tw:宣告;&lt;br /&gt;
}}&lt;br /&gt;
{{lowercase|typedef}}&lt;br /&gt;
在[[C語言|C]]和[[C++]][[程式語言]]中，&amp;lt;code&amp;gt;&amp;#039;&amp;#039;&amp;#039;typedef&amp;#039;&amp;#039;&amp;#039;&amp;lt;/code&amp;gt;是一個[[保留字|關鍵字]]。它用來對一個[[資料類型]]取一個別名，目的是為了使[[原始碼]]更易於閱讀和理解。它通常用於簡化宣告複雜的類型組成的結構 ，但它也常常在各種長度的整數資料型別中看到，例如&amp;lt;code&amp;gt;size_t&amp;lt;/code&amp;gt;和&amp;lt;code&amp;gt;time_t&amp;lt;/code&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 語法 ==&lt;br /&gt;
&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;的語法是 : &amp;lt;code&amp;gt;&amp;#039;&amp;#039;&amp;#039;typedef&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;typedeclaration&amp;#039;&amp;#039;;&amp;lt;/code&amp;gt;&amp;lt;ref&amp;gt;{{cite web|url=http://en.cppreference.com/w/cpp/keyword/typedef|title=typedef specifier|website=cppreference.com|access-date=18 June 2016|archive-date=2018-03-10|archive-url=https://web.archive.org/web/20180310010008/http://en.cppreference.com/w/cpp/keyword/typedef|dead-url=no}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
創建 &amp;lt;code&amp;gt;Length&amp;lt;/code&amp;gt; 作為 &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; 的別名 : &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;typedef int Length;&amp;lt;/syntaxhighlight&amp;gt;創建 &amp;lt;code&amp;gt;PFI&amp;lt;/code&amp;gt; 作為一個指向「一個擁有兩個 &amp;lt;code&amp;gt;char *&amp;lt;/code&amp;gt; 當作參數並回傳 &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; 的函式」的指標的別名&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;typedef int (*PFI)(char *, char *);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 使用範例 ==&lt;br /&gt;
&lt;br /&gt;
=== 創建一個具有特別意義的資料型別 ===&lt;br /&gt;
&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;會被用來指定一種資料型別的意義。&lt;br /&gt;
&lt;br /&gt;
例如 :  以下示範一個普通的宣告，速度及分數都被宣告為&amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
int current_speed ; &lt;br /&gt;
int high_score ; &lt;br /&gt;
&lt;br /&gt;
void congratulate(int your_score) &lt;br /&gt;
{ &lt;br /&gt;
    if (your_score &amp;gt; high_score) &lt;br /&gt;
        ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;通過&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;來指定新的資料型別的意義:&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
typedef int km_per_hour ;&lt;br /&gt;
typedef int points ;&lt;br /&gt;
&lt;br /&gt;
km_per_hour current_speed ;  //&amp;quot;km_per_hour&amp;quot; is synonymous with &amp;quot;int&amp;quot; here,&lt;br /&gt;
points high_score ;          //and thus, the compiler treats our new variables as integers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void congratulate(points your_score) {&lt;br /&gt;
    if (your_score &amp;gt; high_score)&lt;br /&gt;
        ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;前面兩段程式碼運作狀況一樣，但是使用&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;的第二段程式碼更清楚的表示了兩個變數(score和speed)，雖然資料型別都是&amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;，卻是各自代表不同的意義，且他們的資料並不相容。&lt;br /&gt;
&lt;br /&gt;
但請注意，其清楚的表示不同意義只是對於工程師而言，C/C++的編譯器認為兩個變數都是&amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;時，並不會顯示警告或錯誤，如: 以下程式碼，使用宣告為速度的變數作為&amp;lt;code&amp;gt;congratulate&amp;lt;/code&amp;gt;函式的參數 : &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
void foo() &lt;br /&gt;
{ &lt;br /&gt;
    km_per_hour km100 = 100; &lt;br /&gt;
    congratulate(km100);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
但是，雖然在上面的程式碼中，編譯器認為&amp;quot;km_per_hour&amp;quot;等於&amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;，但在使用前綴 &amp;lt;code&amp;gt;unsigned&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;signed&amp;lt;/code&amp;gt;時，兩者並不能互換使用。&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
void foo() {&lt;br /&gt;
    unsigned int a;         // Okay&lt;br /&gt;
    unsigned km_per_hour b; // Compiler complains&lt;br /&gt;
    long int c;             // Okay&lt;br /&gt;
    long km_per_hour d;     // Compiler complains&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
另一个例子：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int coxes;&lt;br /&gt;
int jaffa;&lt;br /&gt;
...&lt;br /&gt;
coxes++;&lt;br /&gt;
...&lt;br /&gt;
if (jaffa == 10)&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
現在來看以下程式碼：&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
typedef int Apple;&lt;br /&gt;
typedef int Orange;&lt;br /&gt;
Apple coxes;&lt;br /&gt;
Orange jaffa;&lt;br /&gt;
...&lt;br /&gt;
coxes++;&lt;br /&gt;
...&lt;br /&gt;
if (jaffa == 10)&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
這兩段程式碼都做同樣的一件事。第二個例子使用了 &amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;，使其更易於了解將要進行什麼。也就是一個變數包含關於蘋果的資訊，而另一個包含關於橘子的資訊。&lt;br /&gt;
&lt;br /&gt;
=== 簡化宣告語法 ===&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct var {&lt;br /&gt;
    int data1;&lt;br /&gt;
    int data2;&lt;br /&gt;
    char data3;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
此處使用者定義一個資料類型 &amp;#039;&amp;#039;&amp;lt;code&amp;gt;var&amp;lt;/code&amp;gt;&amp;#039;&amp;#039;。&lt;br /&gt;
&lt;br /&gt;
像這樣建立一個 &amp;#039;&amp;#039;&amp;lt;code&amp;gt;var&amp;lt;/code&amp;gt;&amp;#039;&amp;#039; 類型的變數，程式碼必須寫為（注意，在 C++ 中宣告一個 &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; 時，同時也隱含了 &amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;，C 則沒有）：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct var a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
在例子的最末處加入一行語句：&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct var newtype;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
現在要建立類型 &amp;#039;&amp;#039;&amp;lt;code&amp;gt;var&amp;lt;/code&amp;gt;&amp;#039;&amp;#039; 的變數時，程式碼可以寫為：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
newtype a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
這樣就更容易閱讀了，因為不用再為每一個 &amp;#039;&amp;#039;var&amp;#039;&amp;#039; 類型的變數加上關鍵字 &amp;#039;&amp;#039;&amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt;&amp;#039;&amp;#039;。&lt;br /&gt;
&lt;br /&gt;
也可以給陣列使用 &amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt; 宣告。&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
typedef BaseType NewType [arrSize];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;這樣就可以在宣告一個 &amp;#039;&amp;#039;&amp;lt;code&amp;gt;BaseType&amp;lt;/code&amp;gt;&amp;#039;&amp;#039; 類型和 &amp;#039;&amp;#039;&amp;lt;code&amp;gt;arrSize&amp;lt;/code&amp;gt;&amp;#039;&amp;#039; 大小的新陣列時，將程式碼寫為：&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
NewType array;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 與陣列一起使用 ===&lt;br /&gt;
&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;可以簡單的跟陣列一起使用。例如 : &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
typedef char arrType[6];    // type name: arrType&lt;br /&gt;
                            // new type: char[6]&lt;br /&gt;
&lt;br /&gt;
arrType arr={1,2,3,4,5,6};  // same as: char arr[6]={1,2,3,4,5,6}&lt;br /&gt;
&lt;br /&gt;
arrType *pArr;              // same as: char (*pArr)[6];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;在這裡，&amp;lt;code&amp;gt;arrType&amp;lt;/code&amp;gt;是&amp;lt;code&amp;gt;char[6]&amp;lt;/code&amp;gt;的別稱。而&amp;lt;code&amp;gt;arrType *pArr;&amp;lt;/code&amp;gt;則表示&amp;lt;code&amp;gt;pArr&amp;lt;/code&amp;gt;是一個指向儲存&amp;lt;code&amp;gt;char[6]&amp;lt;/code&amp;gt;型別記憶體的指標。&lt;br /&gt;
&lt;br /&gt;
=== 與指標一起使用 ===&lt;br /&gt;
可以使用typedef來定義一個新的指標型別 : &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
typedef int *intptr;   // type name: intptr&lt;br /&gt;
                       // new type: int*&lt;br /&gt;
&lt;br /&gt;
intptr ptr;            // same as: int *ptr&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;在上面那段程式碼中，&amp;lt;code&amp;gt;intptr&amp;lt;/code&amp;gt;是一個 指標型態&amp;lt;code&amp;gt;int*&amp;lt;/code&amp;gt;的 新的別名。&amp;lt;code&amp;gt;intptr ptr;&amp;lt;/code&amp;gt;宣告了一個變數(&amp;lt;code&amp;gt;ptr&amp;lt;/code&amp;gt;)，其資料型別是&amp;lt;code&amp;gt;int*&amp;lt;/code&amp;gt;。如此一來&amp;lt;code&amp;gt;ptr&amp;lt;/code&amp;gt;就是一個 可以指向一段儲存&amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;資料的記憶體 的指標了。&lt;br /&gt;
&lt;br /&gt;
使用&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;來定義一個新的指標型別有時候會造成一些困惑 : &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
typedef int *intptr;&lt;br /&gt;
&lt;br /&gt;
intptr cliff, allen;        // both cliff and allen are int* type&lt;br /&gt;
&lt;br /&gt;
intptr cliff2, *allen2;     // cliff2 is int* type, but allen2 is int** type&lt;br /&gt;
                            // same as: intptr cliff2;&lt;br /&gt;
                            //          intptr *allen2;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;在上面的程式碼中,&amp;#039;&amp;#039;&amp;lt;code&amp;gt;ntptr cliff, allen;&amp;lt;/code&amp;gt;&amp;#039;&amp;#039;表示宣告兩個變數，其資料型別是&amp;lt;code&amp;gt;int*&amp;lt;/code&amp;gt;，而&amp;lt;code&amp;gt;intptr *allan2&amp;lt;/code&amp;gt;則使&amp;lt;code&amp;gt;allen2&amp;lt;/code&amp;gt;的型別成為&amp;lt;code&amp;gt;int**&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 與結構指標一起使用 ===&lt;br /&gt;
&amp;lt;code&amp;gt;Typedef&amp;lt;/code&amp;gt;可以跟[[結構體 (C語言)|結構體]][[指標 (電腦科學)|指標]]一起使用。如下 : &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
struct Node {&lt;br /&gt;
    int data;&lt;br /&gt;
    struct Node *nextptr;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;使用&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;可以改寫成如下 : &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct Node Node;&lt;br /&gt;
struct Node {&lt;br /&gt;
    int data;&lt;br /&gt;
    Node *nextptr;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;在[[C語言]]中，可以在一行中宣告複數的變數，不管其是不是指標。不管如何，如果你要宣告指標，必須在每個變數前面加上星號。&lt;br /&gt;
&lt;br /&gt;
在下面的程式碼中，工程師可能會以為&amp;lt;code&amp;gt;errptr&amp;lt;/code&amp;gt;是一個指標，這個可能會引起一些錯誤。&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
struct Node *startptr, *endptr, *curptr, *prevptr, errptr, *refptr;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;如果你用&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;定義一個&amp;lt;code&amp;gt;Node *&amp;lt;/code&amp;gt;，這可以保證所有的變數都是一個指向一個&amp;lt;code&amp;gt;structure type&amp;lt;/code&amp;gt;的指標。&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct Node* NodePtr;&lt;br /&gt;
...&lt;br /&gt;
NodePtr startptr, endptr, curptr, prevptr, errptr, refptr;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 與函式指標一起使用 ===&lt;br /&gt;
先看看以下這段尚未使用&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;的程式碼：&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
int do_math(float arg1, int arg2) {&lt;br /&gt;
    return arg2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int call_a_func(int (*call_this)(float, int)) {&lt;br /&gt;
    int output = call_this(5.5, 7);&lt;br /&gt;
    return output;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int final_result = call_a_func(&amp;amp;do_math);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;這段程式碼可以被改寫成如下：&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
typedef int (*MathFunc)(float, int);&lt;br /&gt;
&lt;br /&gt;
int do_math(float arg1, int arg2) {&lt;br /&gt;
    return arg2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int call_a_func(MathFunc call_this) {&lt;br /&gt;
    int output = call_this(5.5, 7);&lt;br /&gt;
    return output;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int final_result = call_a_func(&amp;amp;do_math);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;在這裡，&amp;lt;code&amp;gt;MathFunc&amp;lt;/code&amp;gt;是一個指標，指向一個回傳&amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;並以一個&amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;和一個&amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;作為參數使用的函式。當一個函式被當作參數使用時，如果少了&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;它可能會變得難以了解。&lt;br /&gt;
&lt;br /&gt;
以下是&amp;lt;code&amp;gt;signal(3)&amp;lt;/code&amp;gt;(來自[[FreeBSD]])的函數原型：&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
void (*signal(int sig, void (*func)(int)))(int);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;上面宣告的函式相當的神祕，因為它沒有清楚的顯示它以什麼函式當作參數，或回傳了什麼資料型別。一個初心者工程師甚至可能以為它接收一個&amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;作為參數，並不回傳任何東西。但它其實接收了一個&amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;和一個&amp;lt;code&amp;gt;function pointer&amp;lt;/code&amp;gt;作為參數，並回傳了一個&amp;lt;code&amp;gt;function pointer&amp;lt;/code&amp;gt;。它可以被改寫成以下程式碼：&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
typedef void (*sighandler_t)(int);&lt;br /&gt;
sighandler_t signal(int sig, sighandler_t func);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  用來型別轉換  ===&lt;br /&gt;
&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;同時可以用來[[類型轉換]]。例如：&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
typedef int (*funcptr)(double);         // pointer to function taking a double returning int&lt;br /&gt;
funcptr x = (funcptr) NULL;             // C or C++&lt;br /&gt;
funcptr y = funcptr(NULL);              // C++ only&lt;br /&gt;
funcptr z = static_cast&amp;lt;funcptr&amp;gt;(NULL); // C++ only&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;左側，&amp;lt;code&amp;gt;funcptr&amp;lt;/code&amp;gt;用來宣告變數；右側，&amp;lt;code&amp;gt;funcptr&amp;lt;/code&amp;gt;則用來轉換值的型態。&lt;br /&gt;
&lt;br /&gt;
如果少了&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;，替換使用宣告語法和型別轉換語法是幾乎不能做到的。例如：&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
void *p = NULL;&lt;br /&gt;
int (*x)(double)  = (int (*)(double)) p; // This is legal&lt;br /&gt;
int (*)(double) y = (int (*)(double)) p; // Left-hand side is not legal&lt;br /&gt;
int (*z)(double)  = (int (*p)(double));  // Right-hand side is not legal&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 外部連結 ==&lt;br /&gt;
*[http://www.cprogramming.com/tutorial/typedef.html Cprogramming.com]{{Wayback|url=http://www.cprogramming.com/tutorial/typedef.html |date=20070402152721 }} - 詳細的討論&lt;br /&gt;
&lt;br /&gt;
== 參照 ==&lt;br /&gt;
*[[抽象資料型別]]&lt;br /&gt;
*[[C语言语法]]&lt;br /&gt;
&lt;br /&gt;
[[Category:C語言]]&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Sakurase</name></author>
	</entry>
</feed>