gpt4 book ai didi

c语言关于typedef的一些事情

转载 作者:行者123 更新时间:2023-11-30 20:36:36 25 4
gpt4 key购买 nike

"In effect, typedef is like #define, except that since it is interpreted by 
the compiler, it can cope with textual substitutions that are beyond the
capabilities of the preprocessor."

上面这句话怎么理解?关于typedef有两个用法我无法理解:

1. typedef char Line[10];
2. typedef int (*p)(char*,char*)

在我看来 typedef A B 意味着 B 是 A 的别名,所以“typedef char Line[10]” Line[10] 是 char 的别名!? (p)(char,char*) 是 int 的别名!?不!很明显不是!谁能解释一下吗?

最佳答案

typedef 和宏之间的一个区别是 typedef 可以以宏不能的方式包含声明符信息。例如,假设您有:

int (*p)(int, double);

这将 p 声明为指向 int 值函数的指针,该函数采用 intdouble > 作为参数。

使用typedef,您可以定义与此完全匹配的新数据类型:

typedef int (*t)(int, double);

然后您可以获得 p 的等效定义:

t p;

请注意,您仍然可以使用 t 的完整声明符语法。例如,您可以:

t q[10];

这将声明一个包含 10 个 t 类型元素的数组。如果没有 typedef,这将是:

int (*q[10])(int, double);

关于c语言关于typedef的一些事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35591853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com