2010-04-02 51 views

回答

24

維基百科:

的typedef是在C和C++編程語言的關鍵字。 typedef的目的是爲現有類型指定替代名稱,通常是那些標準聲明繁瑣,可能會令人困惑或可能因實現而異的標準聲明。

和:

ķ& [R指出,有兩個原因,使用一個typedef。首先,它提供了使程序更加便攜的手段。無需在整個程序的源文件中隨處改變類型,只需要更改一個typedef語句。其次,typedef可以使複雜的聲明更容易理解。

和反對參數:

他(格雷格K.H.)認爲,這種做法不僅不必要的混淆代碼,它也可能造成程序員不小心誤用大型結構想着他們是簡單類型。

35

typedef用於定義類型。例如:

typedef struct { 
    int a; 
    int b; 
} THINGY; 

...將THINGY定義爲給定的結構。這樣一來,你可以使用它像這樣:

THINGY t; 

...而不是:

struct _THINGY_STRUCT { 
    int a; 
    int b; 
}; 

struct _THINGY_STRUCT t; 

...這是更詳細一點。 typedefs可以使一些事情更加清晰,特別指向函數。

+1

是否有沒有理由不'typedef'結構'然後呢? – Panzercrisis 2014-09-28 04:37:45

+1

@Panzercrisis:我不知道其中的一個,當然是我經常做〜1989 - 1998年(我做了大部分C工作),但我不是C專家(不再)。 – 2014-09-28 08:08:35

3

它可以別名另一種類型。

typedef unsigned int uint; /* uint is now an alias for "unsigned int" */ 
12

類型定義是用於創建別名現有類型。這是一個misnomer:typedef沒有定義新的類型,因爲新的類型可以與基礎類型互換。當基礎類型可能發生變化或不重要時,Typedef通常用於接口定義的清晰性和可移植性。

例如:

// Possibly useful in POSIX: 
typedef int filedescriptor_t; 

// Define a struct foo and then give it a typedef... 
struct foo { int i; }; 
typedef struct foo foo_t; 

// ...or just define everything in one go. 
typedef struct bar { int i; } bar_t; 

// Typedef is very, very useful with function pointers: 
typedef int (*CompareFunction)(char const *, char const *); 
CompareFunction c = strcmp; 

還的typedef可以用來給名不願透露姓名的類型。在這種情況下,typedef的將是所述類型的唯一名稱:

typedef struct { int i; } data_t; 
typedef enum { YES, NO, FILE_NOT_FOUND } return_code_t; 

命名慣例不同。通常建議使用trailing_underscore_and_tCamelCase

3

typedef沒有引入新類型,但它只是爲類型提供了一個新名稱。

TYPEDEF可用於:

  1. 類型的結合數組,結構指針或功能。

  2. 爲了便於攜帶,您需要的類型爲typedef。然後,當您將代碼移植到不同平臺時,只需在typedef中進行更改即可選擇正確的類型。

  3. A typedef可以爲複雜的類型轉換提供簡單的名稱。

  4. typedef也可用於給未命名類型提供名稱。在這種情況下,typedef將是所述類型的唯一名稱。

注: -不應該使用TYPEDEF對於結構。即使不需要,也要在結構定義中使用標籤。

+2

請問您是否可以擴展不使用帶結構的typedef? – 2014-07-30 17:22:45

3

維基百科: 「... K & [R指出,有兩個原因,使用一個typedef首先......其次,一個typedef可以使複雜的聲明更容易理解」

下面是一個例子,以使用的typedef,簡化複雜類型的第二個原因(複雜類型是選自K & R 2連「C編程語言第二版第136頁)。

char (*(*x([])()) 

x是返回指針數組的函數[]指針的函數返回炭。

我們可以使上面的聲明可以理解使用的typedef。請參考下面的例子中

typedef char (*pfType)(); // pf is the type of pointer to function returning 
          // char 
typedef pfType pArrType[2]; // pArr is the type of array of pointers to 
          // functions returning char 

char charf() 
{ return('b'); 
} 

pArrType pArr={charf,charf}; 
pfType *FinalF()  // f is a function returning pointer to array of 
        // pointer to function returning char 
{ 
return(pArr); 
} 
1

在下面的例子中解釋typedef的用法。此外,Typedef用於使代碼更具可讀性。

#include <stdio.h> 
#include <math.h> 

/* 
To define a new type name with typedef, follow these steps: 
1. Write the statement as if a variable of the desired type were being declared. 
2. Where the name of the declared variable would normally appear, substitute the new type name. 
3. In front of everything, place the keyword typedef. 
*/ 

// typedef a primitive data type 
typedef double distance; 

// typedef struct 
typedef struct{ 
    int x; 
    int y; 
} point; 

//typedef an array 
typedef point points[100]; 

points ps = {0}; // ps is an array of 100 point 

// typedef a function 
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point) 

// prototype a function  
distance findDistance(point, point); 

int main(int argc, char const *argv[]) 
{ 
    // delcare a function pointer 
    distanceFun_p func_p; 

    // initialize the function pointer with a function address 
    func_p = findDistance; 

    // initialize two point variables 
    point p1 = {0,0} , p2 = {1,1}; 

    // call the function through the pointer 
    distance d = func_p(p1,p2); 

    printf("the distance is %f\n", d); 

    return 0; 
} 

distance findDistance(point p1, point p2) 
{ 
distance xdiff = p1.x - p2.x; 
distance ydiff = p1.y - p2.y; 

return sqrt((xdiff * xdiff) + (ydiff * ydiff)); 
} In front of everything, place the keyword typedef. 
    */