invalid conversion from `int (*)(int)' to `void (*)(int)',这个怎么转换

robert_hahaha 2008-01-28 01:12:36
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

int catch1(int sig);
int main(void)
{
signal(SIGINT, catch1); /* 将SIGINT 信号与catch 函数关联 */
printf("xixi\n");
sleep(10);
printf("end\n");
return 0;
}

int catch1(int sig)
{

printf("Catch succeed!\n");

}

[root@zouxd network]# make
gcc page532.cpp -o page532
page532.cpp: In function `int main()':
page532.cpp:9: invalid conversion from `int (*)(int)' to `void (*)(int)'
make: *** [page532] Error 1
[root@zouxd network]#
...全文
1427 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gladstone 2008-05-14
  • 打赏
  • 举报
回复
(void*(*)(int))&catch1
robert_hahaha 2008-01-28
  • 打赏
  • 举报
回复
多谢楼上的
x86 2008-01-28
  • 打赏
  • 举报
回复
也可强制转换:
signal (SIGINT, (void (*)(int))catch1);

或者可读性好一些的:
typedef void (*func_t)(int);
...
signal (SIGINT, (func_t)catch1);
yecheng_110 2008-01-28
  • 打赏
  • 举报
回复
#include <signal.h>

void (*signal(int signo, void (*func)(int)))(int);

信号处理函数的原型:
void (*func)(int);
robert_hahaha 2008-01-28
  • 打赏
  • 举报
回复
把catch1换成void是可以的,但书上是INT,我想知道SIGNAL处理函数如果返回INT,能不能转换成void (*)(int)
cceczjxy 2008-01-28
  • 打赏
  • 举报
回复

int catch1(int sig);
void catch1(int sig);


--------------------------------------------------,
int catch1(int sig)
{

printf("Catch succeed!\n");

}


void catch1(int sig)
{

printf("Catch succeed!\n");

}
yecheng_110 2008-01-28
  • 打赏
  • 举报
回复
不是如何转换的问题
而是函数返回值类型错了
yecheng_110 2008-01-28
  • 打赏
  • 举报
回复
int catch1(int sig) => void catch1(int sig)

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧