pthread_create函数的第三个参数加不加&有什么区别?


   
  #include <stdio.h>
  
#include <pthread.h>

void *pthread_fun(void *arg)
{
while (1) {
sleep(1);
printf("pthread running\n");
}

return((void *)0);
}

int main()
{
pthread_t tid;
pthread_create(&tid, NULL, pthread_fun, NULL);
// pthread_create(&tid, NULL, &pthread_fun, NULL);

while (1) {
sleep(2);
printf("main runing\n");
}
return 0;
}

pthread_create的第三个参数,有的书加&,有的书不加,
有什么区别吗,请教大家!

c

Nixoo 11 years, 5 months ago

取函数地址时函数名前加&与不加无所谓,函数名本来就代表了函数的入口地址。没有什么区别。

例如下面这段代码:

   
  #include <stdio.h>
  
#include <string.h>
#include <stdlib.h>

void test()
{
printf("test\n");
}

int main(int argc, char *argv[])
{
printf("%p\n",test);
printf("%p\n",&test);
}

输出结果为:

0x8048414
0x8048414

可见,其实两者是一样的,都指向着这个函数的入口地址。

超薄浮点草莓味 answered 11 years, 5 months ago

Your Answer