免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9576 | 回复: 10

[C] 使用函数指针时,该函数的参数如何传递 [复制链接]

论坛徽章:
0
发表于 2014-04-17 18:11 |显示全部楼层
例如:
void debug(int val)
{
    printf("val=%d\n", val);
    return;
}

int main()
{
    int second = 5;
   
    /*5秒钟之后打印val*/
    eloop_register(second, &debug);

    return 0;
}


想问下,怎么才能将val的值传过去呢?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2014-04-17 19:42 |显示全部楼层
(*debug)(1);

论坛徽章:
0
发表于 2014-04-17 19:43 |显示全部楼层
eloop_register的原型应该是return_type eloop_register(int, void (*f)(int));
所以参数应该在eloop_register函数里提供的给指针f进行调用函数debug。

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2014-04-17 19:52 |显示全部楼层
你要给eloop_register(second, &debug);再加个参数,将val传进去

论坛徽章:
4
白羊座
日期:2013-09-17 21:59:30技术图书徽章
日期:2013-10-12 22:16:03白羊座
日期:2013-10-14 11:01:40双子座
日期:2013-12-17 18:26:39
发表于 2014-04-17 20:03 |显示全部楼层
c or c++?

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2014-04-17 22:18 |显示全部楼层
回复 4# folklore


    q你已经将second传过去了, 直接
debug(sencond);不就完了。

论坛徽章:
0
发表于 2014-04-18 09:28 |显示全部楼层
本帖最后由 luoops 于 2014-04-18 09:33 编辑

可能我没表达清楚,我是想问debug这个函数的参数怎么传过去?
我自己写的eloop_register,代码如下:
void init_sig_action(void (*handler)(int))
{
        struct sigaction sig_act;

        if(NULL == handler) {
                return;
        }

        sig_act.sa_handler = handler;
        sig_act.sa_flags = 0;

        sigemptyset(&sig_act.sa_mask);
        sigaction(SIGALRM, &sig_act, NULL);

        return;
}

void init_time(int seconds)
{
        struct itimerval timer;

        timer.it_value.tv_sec = seconds;
        timer.it_value.tv_usec = 0;
        timer.it_interval = timer.it_value;

        setitimer(ITIMER_REAL, &timer, NULL);

}

void eloop_register_timer(int seconds, void (*handler)(int))
{
        if((seconds <= 0) || (NULL == handler)) {
                return;
        }

        init_sig_action(handler);
        init_time(seconds);

        return;
}
回复 3# libo26_lee


   

论坛徽章:
0
发表于 2014-04-18 09:30 |显示全部楼层
eloop_register是可以加参数,但是debug怎么接受呢?
并不是在eloop_register里面直接调用debug。。。。回复 4# folklore


   

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2014-04-18 14:10 |显示全部楼层
回复 8# luoops


这个没办法, 信号处理函数基本就不会接受多余的参数。

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
发表于 2014-04-20 09:49 |显示全部楼层
  1. NAME
  2.        sigqueue - queue a signal and data to a process

  3. SYNOPSIS
  4.        #include <signal.h>

  5.        int sigqueue(pid_t pid, int sig, const union sigval value);

  6. DESCRIPTION
  7.        sigqueue()  sends  the signal specified in sig to the process whose PID is given in pid.  The permissions required to send a
  8.        signal are the same as for kill(2).  As with kill(2), the null signal (0) can be used to check if a process with a given PID
  9.        exists.

  10.        The  value  argument  is used to specify an accompanying item of data (either an integer or a pointer value) to be sent with
  11.        the signal, and has the following type:

  12.          union sigval {
  13.              int   sival_int;
  14.              void *sival_ptr;
  15.          };

  16.        If the receiving process has installed a handler for this signal using the SA_SIGINFO flag  to  sigaction(2),  then  it  can
  17.        obtain  this  data via the si_value field of the siginfo_t structure passed as the second argument to the handler.  Further-
  18.        more, the si_code field of that structure will be set to SI_QUEUE.
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP