linux C函数指针的有关问题,编译报警告
linux C函数指针的问题,编译报警告
在gcc下编程,创建多个线程,想将这些线程地址都放在一个指针数组里,
上述代码,编译后报警告:位置为指针函数数组的定义处 共5个警告
warning: initialization from incompatible pointer type
......
对函数指针数组用法不是很熟悉,应该怎样修改实现我的想法,谢谢
------解决方案--------------------
void *thread_1(void *arg);
void (*init_thread[ ])(void *)
少了个*,
void* (*init_thread[ ])(void *)
在gcc下编程,创建多个线程,想将这些线程地址都放在一个指针数组里,
//5个线程的声明
void *thread_1(void *arg);
void *thread_2(void *arg);
void *thread_3(void *arg);
void *thread_4(void *arg);
void *thread_5(void *arg);
//这里想将所有的线程名都放在一个数组里,便于管理
void (*init_thread[ ])(void *) = {
thread_1,
thread_2,
thread_3,
thread_4,
thread_5
};
pthread_t id[5];//线程id
for (i=0; i<MAX_THREAD_CLIENT; i++) {//通过循环的方式实现创建线程
counts = 0;
while(pthread_create(&id[i], NULL, init_thread[i], NULL)) {//创建线程
if (counts++ > 100) {
fprintf(stderr, "thread[%d]: failed to create\n", i);
exit(EXIT_FAILURE);
}
}
counts = 0;
while(pthread_join(id[i], NULL)) {//加入线程
if (counts++ > 100) {
fprintf(stderr, "thread[%d]: failed to join\n", i);
exit(EXIT_FAILURE);
}
}
}
上述代码,编译后报警告:位置为指针函数数组的定义处 共5个警告
warning: initialization from incompatible pointer type
......
对函数指针数组用法不是很熟悉,应该怎样修改实现我的想法,谢谢
------解决方案--------------------
void *thread_1(void *arg);
void (*init_thread[ ])(void *)
少了个*,
void* (*init_thread[ ])(void *)