typedef
と はC でも同じでしょうか#define
。 それらの違いは何でしょうか。
ベストアンサー1
typedef
は変数と同様にスコープ規則に従いますが、 はdefine
コンパイル単位の終わりまで(または一致する までundef
)有効です。
また、 では実行できてtypedef
も、 では実行できないことがありますdefine
。
例:
typedef int* int_p1;
int_p1 a, b, c; // a, b, c are all int pointers
#define int_p2 int*
int_p2 a, b, c; // only the first is a pointer, because int_p2
// is replaced with int*, producing: int* a, b, c
// which should be read as: int *a, b, c
typedef int a10[10];
a10 a, b, c; // create three 10-int arrays
typedef int (*func_p) (int);
func_p fp; // func_p is a pointer to a function that
// takes an int and returns an int