Typedef
在C和C++程式語言中, 語法
創建 typedef int Length;
創建 typedef int (*PFI)(char *, char *);
使用範例創建一個具有特別意義的資料型別
例如 : 以下示範一個普通的宣告,速度及分數都被宣告為 int current_speed ;
int high_score ;
void congratulate(int your_score)
{
if (your_score > high_score)
...
通過 typedef int km_per_hour ;
typedef int points ;
km_per_hour current_speed ; //"km_per_hour" is synonymous with "int" here,
points high_score ; //and thus, the compiler treats our new variables as integers.
void congratulate(points your_score) {
if (your_score > high_score)
...
前面兩段程式碼運作狀況一樣,但是使用 void foo()
{
km_per_hour km100 = 100;
congratulate(km100);
但是,雖然在上面的程式碼中,編譯器認為"km_per_hour"等於 void foo() {
unsigned int a; // Okay
unsigned km_per_hour b; // Compiler complains
long int c; // Okay
long km_per_hour d; // Compiler complains
另一个例子: int coxes;
int jaffa;
...
coxes++;
...
if (jaffa == 10)
...
現在來看以下程式碼: typedef int Apple;
typedef int Orange;
Apple coxes;
Orange jaffa;
...
coxes++;
...
if (jaffa == 10)
...
這兩段程式碼都做同樣的一件事。第二個例子使用了 簡化宣告語法struct var {
int data1;
int data2;
char data3;
};
此處使用者定義一個資料類型 像這樣建立一個 struct var a;
typedef struct var newtype;
現在要建立類型 newtype a;
這樣就更容易閱讀了,因為不用再為每一個 var 類型的變數加上關鍵字
也可以給陣列使用 typedef BaseType NewType [arrSize];
這樣就可以在宣告一個 NewType array;
與陣列一起使用
typedef char arrType[6]; // type name: arrType
// new type: char[6]
arrType arr={1,2,3,4,5,6}; // same as: char arr[6]={1,2,3,4,5,6}
arrType *pArr; // same as: char (*pArr)[6];
在這裡, 與指標一起使用可以使用typedef來定義一個新的指標型別 : typedef int *intptr; // type name: intptr
// new type: int*
intptr ptr; // same as: int *ptr
在上面那段程式碼中, typedef int *intptr;
intptr cliff, allen; // both cliff and allen are int* type
intptr cliff2, *allen2; // cliff2 is int* type, but allen2 is int** type
// same as: intptr cliff2;
// intptr *allen2;
在上面的程式碼中, 與結構指標一起使用struct Node {
int data;
struct Node *nextptr;
};
使用 typedef struct Node Node;
struct Node {
int data;
Node *nextptr;
};
在C語言中,可以在一行中宣告複數的變數,不管其是不是指標。不管如何,如果你要宣告指標,必須在每個變數前面加上星號。
在下面的程式碼中,工程師可能會以為 struct Node *startptr, *endptr, *curptr, *prevptr, errptr, *refptr;
如果你用 typedef struct Node* NodePtr;
...
NodePtr startptr, endptr, curptr, prevptr, errptr, refptr;
與函式指標一起使用
先看看以下這段尚未使用 int do_math(float arg1, int arg2) {
return arg2;
}
int call_a_func(int (*call_this)(float, int)) {
int output = call_this(5.5, 7);
return output;
}
int final_result = call_a_func(&do_math);
這段程式碼可以被改寫成如下: typedef int (*MathFunc)(float, int);
int do_math(float arg1, int arg2) {
return arg2;
}
int call_a_func(MathFunc call_this) {
int output = call_this(5.5, 7);
return output;
}
int final_result = call_a_func(&do_math);
在這裡, void (*signal(int sig, void (*func)(int)))(int);
上面宣告的函式相當的神祕,因為它沒有清楚的顯示它以什麼函式當作參數,或回傳了什麼資料型別。一個初心者工程師甚至可能以為它接收一個 typedef void (*sighandler_t)(int);
sighandler_t signal(int sig, sighandler_t func);
用來型別轉換
typedef int (*funcptr)(double); // pointer to function taking a double returning int
funcptr x = (funcptr) NULL; // C or C++
funcptr y = funcptr(NULL); // C++ only
funcptr z = static_cast<funcptr>(NULL); // C++ only
左側, void *p = NULL;
int (*x)(double) = (int (*)(double)) p; // This is legal
int (*)(double) y = (int (*)(double)) p; // Left-hand side is not legal
int (*z)(double) = (int (*p)(double)); // Right-hand side is not legal
外部連結
參照
Information related to Typedef |