C++ C/C++;

C++ C/C++;,c++,c,function-pointers,C++,C,Function Pointers,我最近在读Linux内核。 我发现在许多情况下,他们使用结构“typedef xxx f(xxx)”,但我无法理解它是如何工作的。(类似于函数指针?) 这是我的测试代码 #include<stdio.h> typedef int Myfunc(int); typedef int (*point_to_myfunc)(int); static Myfunc example; static int example(int a){ printf("example a=%d\n",

我最近在读Linux内核。 我发现在许多情况下,他们使用结构“typedef xxx f(xxx)”,但我无法理解它是如何工作的。(类似于函数指针?)

这是我的测试代码

#include<stdio.h>
typedef int Myfunc(int);
typedef int (*point_to_myfunc)(int);
static Myfunc example;
static int example(int a){
    printf("example a=%d\n", a);
    return 1;
}
static void example2(Myfunc* f){
    printf("example2\n");
    f(2);
}
static void example3(int (*)(int));
static void example3(int (*point_to_Myfunc)(int)){
    printf("example3\n");
    point_to_Myfunc(3);
}
int main(){
    point_to_myfunc f=&example;
    example2(f);
    example3(f);
    return 0;
}
#包括
typedef int Myfunc(int);
typedef int(*点到点)(int);
静态Myfunc示例;
静态int示例(int a){
printf(“示例a=%d\n”,a);
返回1;
}
静态无效示例2(Myfunc*f){
printf(“示例2\n”);
f(2);
}
静态无效示例3(int(*)(int));
静态无效示例3(int(*point_to_Myfunc)(int)){
printf(“示例3\n”);
点到点(3);
}
int main(){
点到点f=&example;
例2(f);
例3(f);
返回0;
}
有人能给我一个简短的解释吗?Thx~

typedef int Myfunc(int);
这意味着Myfunc是一种接受int参数并返回int的函数类型

这一行:

static Myfunc example;
就是说

static int example(int);
哪个forward声明了示例函数

这样做的一个用途是更清楚地说明特定的一组函数用于特定的目的

typedef char CharacterConverter(char);

extern CharacterConverter make_upper_case;
extern CharacterConverter make_lower_case;

extern void process_string(char *s,CharacterConverter *f);
    // easier to see that make_upper_case and make_lower_case are valid arguments.
这意味着Myfunc是一种接受int参数并返回int的函数类型

这一行:

static Myfunc example;
就是说

static int example(int);
哪个forward声明了示例函数

这样做的一个用途是更清楚地说明特定的一组函数用于特定的目的

typedef char CharacterConverter(char);

extern CharacterConverter make_upper_case;
extern CharacterConverter make_lower_case;

extern void process_string(char *s,CharacterConverter *f);
    // easier to see that make_upper_case and make_lower_case are valid arguments.

定义类型时,typedef很有用

例如:
char*a,b定义了指针“a”和字符b。
char*a和*b
定义了两个char指针。 如果使用typedef,则会清除:

typedef char* PCHAR;
PCHAR a,b;
现在,a和b都是字符指针

typedef int Myfunc(int);
typedef int (*point_to_myfunc)(int);

这两行定义了一对、一种函数格式和一种可以指向函数的指针类型,因此在使用它们时会更加清晰和明显。

typedef在定义类型时非常有用

例如:
char*a,b定义了指针“a”和字符b。
char*a和*b
定义了两个char指针。 如果使用typedef,则会清除:

typedef char* PCHAR;
PCHAR a,b;
现在,a和b都是字符指针

typedef int Myfunc(int);
typedef int (*point_to_myfunc)(int);
这两行定义了一对、一种函数格式和一种可以指向函数的指针类型,因此使用它们时会更加清晰和明显。

Vaughn Cato是正确的, 另外,

typedef int (*point_to_myfunc)(int);
定义一个函数指针,这意味着point_to_myfunc是一个类型,我们可以这样使用它:

point_to_myfunc f=&example;
现在f就像example(),我们可以用f()调用方法example,
#include <stdio.h>
typedef int Myfunc(int);
另外,

typedef int (*point_to_myfunc)(int);
定义一个函数指针,这意味着point_to_myfunc是一个类型,我们可以这样使用它:

point_to_myfunc f=&example;
现在f和example()一样,我们可以用f()调用example方法

#include <stdio.h>
typedef int Myfunc(int);
point\u to_myfunc
是指向函数的指针,该函数使用
int
参数并返回
int
。您还可以有:
typedef Myfunc*ptr_to_Myfunc如果愿意(相同类型的另一个名称)

这表示“存在一个名为
example
的函数,类型为
Myfunc

static int example(int a)
{
    printf("example a=%d\n", a);
    return 1;
}
这是
示例
的可能实现。在该类型函数的定义中,不能使用typedef名称来喜欢
Myfunc

static void example2(Myfunc *f)
{
    printf("example2\n");
    f(2);
}
这是一个指向
Myfunc
的指针的函数。线
f(2)调用参数2指向的函数并忽略返回值

static void example3(int (*)(int));
这将
example3
声明为一个函数,该函数使用指向函数的指针,该函数使用
int
参数并返回
int
结果。它可以写成
static void example3(指向\u到\u myfunc)
静态无效示例3(ptr_to_myfunc)
静态无效示例3(Myfunc*)

这是
示例3
的一个实现

int main(void)
{
    point_to_myfunc f = &example;
    example2(f);
    example3(f);
    return 0;
}
此程序有一个变量
f
,它是指向函数的指针。有趣的是,你可以:

    point_to_myfunc f2 = example;
    point_to_myfunc f3 = *example;
等等,它们的意思都是一样的

您还可以使用以下命令调用它们:

    (*f2)(101);
    (**f3)(103);
初始化的标准符号既不使用
&
也不使用
*
。如果你是一个老派的C程序员,你可以使用
(*f2)(101)
符号调用函数指针;在C89标准之前,这是调用函数指针的唯一方法。现代风格倾向于
f2(101)取而代之

point\u to_myfunc
是指向函数的指针,该函数使用
int
参数并返回
int
。您还可以有:
typedef Myfunc*ptr_to_Myfunc如果愿意(相同类型的另一个名称)

这表示“存在一个名为
example
的函数,类型为
Myfunc

static int example(int a)
{
    printf("example a=%d\n", a);
    return 1;
}
这是
示例
的可能实现。在该类型函数的定义中,不能使用typedef名称来喜欢
Myfunc

static void example2(Myfunc *f)
{
    printf("example2\n");
    f(2);
}
这是一个指向
Myfunc
的指针的函数。线
f(2)调用参数2指向的函数并忽略返回值

static void example3(int (*)(int));
这将
example3
声明为一个函数,该函数使用指向函数的指针,该函数使用
int
参数并返回
int
结果。它可以写成
static void example3(指向\u到\u myfunc)
静态无效示例3(ptr_to_myfunc)
静态无效示例3(Myfunc*)

这是
示例3
的一个实现

int main(void)
{
    point_to_myfunc f = &example;
    example2(f);
    example3(f);
    return 0;
}
此程序有一个变量
f
,它是指向函数的指针。有趣的是,你可以:

    point_to_myfunc f2 = example;
    point_to_myfunc f3 = *example;
等等,它们的意思都是一样的

您还可以使用以下命令调用它们:

    (*f2)(101);
    (**f3)(103);
初始化的标准符号既不使用
&
也不使用
*
。如果你是一个老派的C程序员,你可以使用
(*f2)(101)
符号调用函数指针;在C89标准之前,这是