#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef void(*f_cb)(void);
static f_cb file_options[5]; //等同于static void (*file_options[5])(void); //函数指针数组
void open(void)
{
printf("open file\r\n");
}
void close(void)
{
printf("close file\r\n");
}
void read(void)
{
printf("read file\r\n");
}
void write(void)
{
printf("write file\r\n");
}
int register_function( f_cb callback)
{
int i;
if (callback == NULL)
{
return -1;
}
for(i=0; i<5; i++)
{
if (file_options[i] == NULL)
{
file_options[i] = callback;
break;
}
}
return 0;
}
int main(int argc, char *argv[]) {
int i;
/*函数指针数组*/
/* f_cb file_options[] = {
open,
close,
read,
write
};
*/
register_function(open);
register_function(close);
register_function(read);
register_function(write);
f_cb *action = file_options;
for(i=0 ; i<4; i++)
{
action[i]();
}
/*example one*/
f_cb callback;
callback = open;
(*callback)();
return 0;
}
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef void(*f_cb)(void);
static f_cb file_options[5]; //等同于static void (*file_options[5])(void); //函数指针数组
void open(void)
{
printf("open file\r\n");
}
void close(void)
{
printf("close file\r\n");
}
void read(void)
{
printf("read file\r\n");
}
void write(void)
{
printf("write file\r\n");
}
int register_function( f_cb callback)
{
int i;
if (callback == NULL)
{
return -1;
}
for(i=0; i<5; i++)
{
if (file_options[i] == NULL)
{
file_options[i] = callback;
break;
}
}
return 0;
}
int main(int argc, char *argv[]) {
int i;
/*函数指针数组*/
/* f_cb file_options[] = {
open,
close,
read,
write
};
*/
register_function(open);
register_function(close);
register_function(read);
register_function(write);
f_cb *action = file_options;
for(i=0 ; i<4; i++)
{
action[i]();
}
/*example one*/
f_cb callback;
callback = open;
(*callback)();
return 0;
}