基本的 typedef 語法

typedef 宣告與變數或函式宣告具有相同的語法,但它包含單詞 typedeftypedef 的存在導致宣告宣告一個型別而不是變數或函式。

int T;         // T has type int
typedef int T; // T is an alias for int

int A[100];         // A has type "array of 100 ints"
typedef int A[100]; // A is an alias for the type "array of 100 ints"

一旦定義了型別別名,它就可以與該型別的原始名稱互換使用。

typedef int A[100];
// S is a struct containing an array of 100 ints
struct S {
    A data;
};

typedef 永遠不會創造出獨特的型別。它只提供了另一種引用現有型別的方法。

struct S {
    int f(int);
};
typedef int I;
// ok: defines int S::f(int)
I S::f(I x) { return x; }