关于指针

2015年07月06日

指针解释工具

关于函数指针:

int (*comp)(void *,void *)
comp是一个指向函数的指针,*comp代表一个函数
int *comp(void *,void *)
comp是一个函数,返回一个指向int类型的指针

void *(*start_routine)(void *)
start_routine是一个指向函数*start_routine的指针,返回一个指向void类型的指针,函数的参数是void类型的指针

注意:不同类型的变量赋值之前要进行类型转换

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void *thread_fun(void * arg)
{
    sleep(1);
    int num = *( (int *)arg );
    printf("int the new thread: num = %d\n", num);  
    return NULL;
}
int main(int argc, char *argv[])
{
    pthread_t tid;
    int test = 100;
    pthread_create(&tid, NULL, thread_fun, (void *)&test);  
    while(1);   
    return 0;
}

其中:main函数中test为整型,而pthread_create函数要的是void型的指针,(void *)意为将*转换为void型,&test意为对void型的指针赋值,值为整型变量test的地址