typedef,函数

typedef int Fun() 是什么意思?

今天看到个程序,这样声明个函数:
typedef int Fun() ;
static Fun fun;
函数定义部分为:static int  fun()  { };
为什么不直接声明为:static int fun();
它们之间有什么区别?

-->
renstone9212005-10-11 12:28
typedef int Fun() 是什么意思?

就像
[code]
typedef struct _tagPerson
{
    char name[20];
    int age;
} PersonType;
[/code]
定义了一个结构类型一样,
定义了一个函数类型Fun,所有这个类型的函数都返回一个int类型的值。

据个例子吧,假定有两个分别计算加和减的函数
[code]
int sum(int a, int b)
{
    return (a + b);
}

int sub(int a, int b)
{
    return (a - b);
}

typedef  int CalcType(int a, int b);

int main()
{
    int select = -1;
    int result;
    int a;
    int b;
    CalcType calculate[] = {sum, sub};
    printf("Please select calculation type/n");
    scanf("%d/n", select);
    printf("Input two value/n");
    scanf("%d %d/n", &a, &b);
    if (select == 0 || select == 1)
       result = calculate[select](a, b);
    return (0);
}

另外,通过定义一个函数类型,则可以在函数的参数传递中出入函数类型的参数,以及当你的结构成员需要是函数类型时;还有一些其他的用法。
[/code]
typedef int Fun() ; 
static Fun fun;
首先,定义了一个函数类型Fun, 然后定义了一个模块范围内的Fun类型的静态变量fun。

static int fun();
直接定义了一个模块范围内的函数,在该模块以外不可以引用它。

static 修饰变量和修饰函数的意义是不同的。修饰变量是把该变量的作用与限制在某一范围内,这个范围可以是模块范围、函数范围、类范围、复合语句范围,但其生存期从进入其有效范围开始到程序运行结束。

static修饰函数时,是把这个函数的有效范围限制在定义他的模块内
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值