Chinaunix首页 | 论坛 | 博客
  • 博客访问: 637465
  • 博文数量: 227
  • 博客积分: 8017
  • 博客等级: 中将
  • 技术积分: 2069
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-08 22:50
文章分类

全部博文(227)

文章存档

2011年(10)

2010年(55)

2009年(28)

2008年(134)

我的朋友

分类: C/C++

2008-04-09 20:51:37

一. 举例子说明一下函数指针的用法:



#include <stdio.h>

int fun1(int,int);
void (*fun2(int,int))();

int main(void)
{
     int a=20;
     int b=30;
     int (*pfun1)(int, int); /*定义函数指针*/
     void (*pfun2)(int);     /*定义函数指针*/
     pfun1 = fun1;          /*给函数指针赋值,赋的是函数地址*/
     printf("a>b? %d\n",(*pfun1)(a,b)); /*通过函数指针调用函数*/
     pfun2 = fun2(a,b);      /*接受返回的函数指针*/
     return 0;
}

int fun1(int a, int b)
{
     printf("In fun1:\n");
     return (a>b);
}

void (*fun2(int a, int b))(int){ /*返回函数指针的函数声名*/
     printf("In fun2:\n");
     if(a>b)
         printf("a>b\n");
     else
         printf("a<=b\n");
    
     return (void(*)(int))0;
}



运行结果:
In fun1:
a>b? 0
In fun2:
a<=b

另外:  typedef定义一个函数指针: typedef void (*myfun)(int ) :定义了一个返回值为void ,参数为int的函数指针类型。


二. c语言中的static关键字
1. static 变量:
  • 对于static局部变量: 其作用域为定义它的函数,但生存期为整个程序.也就是,对函数调用完毕后static变量的值一直保存到整个程序结束或者下次修改。 但是不在起作用域内的访问是不允许的
  • 对于statci 全局变量: static 用于限定改全局变量的作用域为本文件,其他程序文件是不可以访问的。
  • static 修饰变量表示是 静态存储方式, 编译器默认初始化为0。(当然全局变量也是静态存储方式)
2. static 函数:
static修饰函数时: 用于限定该函数的作用域为本文件, 所以其他文件的程序是不可以通过extern来访问的。
阅读(1148) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~