2013-03-22 51 views
1

我最近在读Linux内核。 我发现在很多情况下,他们使用struct“typedef xxx f(xxx)”,但我不明白它是如何工作的。 (像函数指针?)关于函数指针在C/C++中的问题

这是我的测试代码。

#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; 
} 

任何人都可以为我提供一个简短的解释吗? Thx〜

+0

是'typedef int Myfunc(int);'甚至合法吗? – Shoe 2013-03-22 05:46:54

+2

@Jeecy为什么不呢?它有一个非常明显的含义,为什么标准不允许它?如果您想知道typedef是否合法,只需删除“typedef”并查看它是否是合法声明。 'int MyFunc(int);'... yup。 – 2013-03-22 05:56:05

回答

3
#include <stdio.h> 
typedef int Myfunc(int); 

Myfunc是一个类型的名称;它是一个函数,参数为int并返回int

typedef int (*point_to_myfunc)(int); 

point_to_myfunc是一个指针指向一个函数获取int参数,并返回一个int。如果您愿意,您也可以有:typedef Myfunc *ptr_to_myfunc;(同一类型的另一个名称)。

static Myfunc example; 

这是说 '存在一个名为Myfunc类型的example功能'。

static int example(int a) 
{ 
    printf("example a=%d\n", a); 
    return 1; 
} 

这是example的可能实现。在该类型的函数的定义中,不能使用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(point_to_myfunc);static void example3(ptr_to_myfunc);static void example3(Myfunc *);

static void example3(int (*point_to_Myfunc)(int)) 
{ 
    printf("example3\n"); 
    point_to_Myfunc(3); 
} 

这是example3的执行。

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);

+0

非常感谢您的明确解释。这个用法非常强大。 – zhoutall 2013-03-22 06:24:57

1
typedef int Myfunc(int); 

这意味着Myfunc是一个函数的类型,它接受一个int参数并返回一个int。

这条线:

static Myfunc example; 

是等于说

static int example(int); 

其中正向声明的示例功能。

这样做的一种用法是使一组特定功能用于特定目的。

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. 
+0

这种typedef的实际用途是什么? – 2013-03-22 05:57:39

+0

@Koushik:我添加了一个可能的用途。 – 2013-03-22 06:10:17

+0

+1。很好。有一个错字。它的characterConverte(r)不是(d)。 – 2013-03-22 06:43:11

0

typedef在定义类型时非常有用。

例如: char *a, b;定义了一个指针“a”和一个char b。 char *a, *b定义了两个字符指针。 如果使用类型定义,这将是明确的:

typedef char* PCHAR; 
PCHAR a,b; 

现在,A和B是一个字符指针。

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

两条线限定的一对,一个功能格式和类型的指针可以指向函数的,所以在使用它们时将清楚和更明显。

+0

OP没有询问'typedef'是什么或者它是什么样的例子。 – 2013-03-22 05:57:17

+0

你说得对,我的错。 – Bing 2013-03-22 06:15:07

1

沃恩卡托是正确的, 此外,

typedef int (*point_to_myfunc)(int); 

定义一个函数指针,它意味着point_to_myfunc是一个类型,我们可以用这样的:

point_to_myfunc f=&example; 

现在f是刚例如(),我们可以f()来调用方法示例