warning: passing argument 3 of 'pthread_create' from incompatible pointer type [-Wincompatible-pointer-types]
expected 'void * (*)(void *)' but argument is of type 'void * (*)(void (*)())'
我想知道代码怎么改能去掉这个提示,望大伙赐教。
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
//回调函数
void OutputSth(const char *str)
{
printf("%s\n", str);
}
//线程函数
void *ThreadRun(void (*func)())
{
const char str[20] = "hello callback.";
int i = 0;
for(; i < 10; ++i)
{
func(str);
sleep(1);
}
return NULL;
}
int main(void)
{
pthread_t thd;
pthread_create(&thd, NULL, ThreadRun, OutputSth);
pthread_join(thd, NULL);
return 0;
}
5 个解决方案
#1
void *ThreadRun(void (*func)())
这个函数的参数类型应该是void*
也就是说是:void *ThreadRun(void * func) 这样的形式
这个函数的参数类型应该是void*
也就是说是:void *ThreadRun(void * func) 这样的形式
#2
pthread_create第三个参数是void *类型,但是void (*func)()函数的返回值是void类型,与void *冲突。
下面的修改,参考一下吧
下面的修改,参考一下吧
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
struct param {
void (*func)(const char *);
};
//回调函数
void OutputSth(const char *str)
{
printf("%s\n", str);
//return NULL;
}
//线程函数
void *ThreadRun(void *arg)
{
const char str[20] = "hello callback.";
struct param *p = (struct param *)arg;
int i = 0;
for(; i < 10; ++i)
{
p->func(str);
sleep(1);
}
return NULL;
}
int main(void)
{
pthread_t thd;
struct param arg;
arg.func = OutputSth;
pthread_create(&thd, NULL, ThreadRun, (void *)&arg);
pthread_join(thd, NULL);
return 0;
}
#3
#4
十分感谢,学习了
#5
线程对应的函数的格式必须是这样:void *xxoo(void *arg)
a.把“void *sayhello(void *arg)”改成“void sayhello(void *arg)”会报错如下:
root@book-desktop:/opt/pc_test/multithreading/1# make
gcc -o main main.c -lpthread
main.c: In function ‘main’:
main.c:14: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’
b.把“void *sayhello(void *arg)”改成“void *sayhello(void)”会报错如下:
root@book-desktop:/opt/pc_test/multithreading/1# make
gcc -o main main.c -lpthread
main.c: In function ‘main’:
main.c:14: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(void)’
a.把“void *sayhello(void *arg)”改成“void sayhello(void *arg)”会报错如下:
root@book-desktop:/opt/pc_test/multithreading/1# make
gcc -o main main.c -lpthread
main.c: In function ‘main’:
main.c:14: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’
b.把“void *sayhello(void *arg)”改成“void *sayhello(void)”会报错如下:
root@book-desktop:/opt/pc_test/multithreading/1# make
gcc -o main main.c -lpthread
main.c: In function ‘main’:
main.c:14: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(void)’
#1
void *ThreadRun(void (*func)())
这个函数的参数类型应该是void*
也就是说是:void *ThreadRun(void * func) 这样的形式
这个函数的参数类型应该是void*
也就是说是:void *ThreadRun(void * func) 这样的形式
#2
pthread_create第三个参数是void *类型,但是void (*func)()函数的返回值是void类型,与void *冲突。
下面的修改,参考一下吧
下面的修改,参考一下吧
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
struct param {
void (*func)(const char *);
};
//回调函数
void OutputSth(const char *str)
{
printf("%s\n", str);
//return NULL;
}
//线程函数
void *ThreadRun(void *arg)
{
const char str[20] = "hello callback.";
struct param *p = (struct param *)arg;
int i = 0;
for(; i < 10; ++i)
{
p->func(str);
sleep(1);
}
return NULL;
}
int main(void)
{
pthread_t thd;
struct param arg;
arg.func = OutputSth;
pthread_create(&thd, NULL, ThreadRun, (void *)&arg);
pthread_join(thd, NULL);
return 0;
}
#3
#4
十分感谢,学习了
#5
线程对应的函数的格式必须是这样:void *xxoo(void *arg)
a.把“void *sayhello(void *arg)”改成“void sayhello(void *arg)”会报错如下:
root@book-desktop:/opt/pc_test/multithreading/1# make
gcc -o main main.c -lpthread
main.c: In function ‘main’:
main.c:14: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’
b.把“void *sayhello(void *arg)”改成“void *sayhello(void)”会报错如下:
root@book-desktop:/opt/pc_test/multithreading/1# make
gcc -o main main.c -lpthread
main.c: In function ‘main’:
main.c:14: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(void)’
a.把“void *sayhello(void *arg)”改成“void sayhello(void *arg)”会报错如下:
root@book-desktop:/opt/pc_test/multithreading/1# make
gcc -o main main.c -lpthread
main.c: In function ‘main’:
main.c:14: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’
b.把“void *sayhello(void *arg)”改成“void *sayhello(void)”会报错如下:
root@book-desktop:/opt/pc_test/multithreading/1# make
gcc -o main main.c -lpthread
main.c: In function ‘main’:
main.c:14: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(void)’