我想知道 c 中的 typedef
和 #define
是否相同?
最佳答案
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
关于c - c中typedef和#define一样吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895489/