今天看pthread_join函数时发现函数的第二个参数是个二级指针,但是打印时用的却是一级指针打印
#include<stdio.h>
#include<pthread.h>
#include"apue.h"
void *thr_fn1(void *arg)
{
//printf("thr_fn1 tid1 = %lu\n",pthread_self());
printf("thread 1 returning\n");
return((void *)1);
}
void *thr_fn2(void *arg)
{
//printf("thr_fn2 tid2 = %lu\n",pthread_self());
printf("thread 2 exiting\n");
pthread_exit((void *)2);
}
int main(void)
{
int err;
pthread_t tid1,tid2;
void *tret;
err = pthread_create(&tid1,NULL,thr_fn1,NULL);
//printf("tid1 =%lu\n",tid1);
if(err != 0)
err_exit(err,"can't creat thread 1");
err = pthread_create(&tid2,NULL,thr_fn2,NULL);
//printf("tid2 =%lu\n",tid2);
if(err != 0)
err_exit(err,"can't creat thread 2");
err = pthread_join(tid1,&tret);
if(err != 0)
err_exit(err,"cant't join with thread 1");
printf("thread 1 exit code %ld\n",(long)tret);
err = pthread_join(tid2,&tret);
if(err != 0)
err_exit(err,"can't join with thread 2");
printf("thread 2 exit code %ld\n",(long)tret);
exit(0);
}
有点疑惑,写一段代码验证下
#include<stdio.h>
int main()
{
int a=1,b=2;
int *p = &a;
int *q = &b;
//p = &a;
//q = &b;
int **pp = &p;
int **qq = &q;
//pp = &p;
//qq = &q;
printf("a = %d\tb = %d\n",a,b);
printf("*p = %d\t*q = %d\n",*p,*q);
printf("p = %p\tq = %p\n",p,q);
printf("*pp = %p\t*qq = %p\n",p,q);
printf("**pp = %d \t**qq = %d\n",**pp,**qq);
printf("pp = %p\tqq = %p\n",pp,qq);
return 0;
}
执行结果
从结果可以看出二级指针的结果确实是通过一级指针来打印的