title: Returning a value from a thread in C
body: <p>Hello i am having a hard time understanding how a value is returned from a thread in c. I have this working example:</p>
  
  <pre><code>#define NTHREADS 4
  void *neg (void * param) {
      int *l;
      l=(int *) param;
      int *r=(int *)malloc(sizeof(int));
      *r=-*l;
      return ((void *) r);
  }
  
  int main (int argc, char *argv[]) {
      pthread_t threads[NTHREADS];
      int arg[NTHREADS];
      int err;
      for(long i=0;i&lt;NTHREADS;i++) {
      arg[i]=i;
      err=pthread_create(&amp;(threads[i]),NULL,&amp;neg,(void *) &amp;(arg[i]));
      if(err!=0)
          error(err,"pthread_create");
      }
      for(int i=0;i&lt;NTHREADS;i++) {
          int *r;
          err=pthread_join(threads[i],(void **)&amp;r);
              printf("Resultat[%d]=%d\n",i,*r);
          free(r);
          if(err!=0)
              error(err,"pthread_join");
      }
      return(EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>What i find hard to understand is the following:</p>
  
  <p>the function neg returns a pointer * r casted in (void *) pointing to a value in the heap. Thus returning an adress to the heap basically. Then in pthread_join we get that return value by doing  &amp;r (which by itself seems illogical? grab the adress of an adress?) then  casting to a pointer of a pointer? Why do we do that?</p>
  
  <p>Thanks for the help!</p>