有人能向我解释一下这个代码片段是如何工作的吗?
typedef int (*compare)(const char*, const char*);
它是指向函数的类型指针的别名声明,该函数具有返回类型int
和两个类型const char *
的参数。
typedef int (*compare)(const char*, const char*);
使用别名,您可以声明指针类型的变量,如下面的演示程序所示
#include <string.h>
#include <stdio.h>
typedef int (*compare)(const char*, const char*);
int main( void )
{
compare cmp = strcmp;
printf( ""Hello" == "Hello" is %sn",
cmp( "Hello", "Hello" ) == 0 ? "true" : "false" );
}
其中strcmp
是像一样声明的标准C字符串函数
int strcmp(const char *s1, const char *s2);
并且指针(变量(CCD_ 4由函数的地址初始化。