title: Thread struct as function parameter C
body: <p>I'm having a trouble passing a struct pointer into a function because I'm a bit confused with those pointers and references. I want to modify the <code>thread.thread_num</code> value from the <code>thread_start</code>function.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt; //malloc, free
  #include &lt;pthread.h&gt;
  
  #define N 5
  
  // void    *malloc(size_t);
  
  struct thread {   
       pthread_t thread_id;       
       int       thread_num;
       // int       thread_sum;       
  };
  
  void *thread_start(void *thread)
  {
     struct thread *my_data;
     my_data = (struct thread *)thread;
     printf("num T: %i\n", my_data-&gt;thread_num);
     my_data-&gt;thread_num=4;
     printf("num T: %i\n", my_data-&gt;thread_num);
  
     return NULL;
  }
  
  int main(int argc, char *argv[])
  {
     int i;
     struct thread  pthread_data;
     struct thread *thread = &amp;pthread_data;
  
     thread-&gt;thread_num=2;
     pthread_create(&amp;thread-&gt;thread_id, NULL, thread_start, (void *)&amp;thread);
     printf("num: %i\n",thread-&gt;thread_num);
  
     pthread_exit(NULL);
     return 0;
  }
  </code></pre>
  
  <p>But the value that print the main doesn't change (2).</p>
  
  <p>And then I want to create an array of thread struct, but I don't know how exactly do that:
  I guess it should be something like this:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
     int i;
     struct thread  pthread_data;
     struct thread *thread[N-1] = &amp;pthread_data; // I don't know how to manage this.
     for(i=0; i&lt;N; i++)
     {
     thread-&gt;thread_num=i;
     pthread_create(&amp;thread[i]-&gt;thread_id, NULL, thread_start, (void *)&amp;thread[i]);
     printf("num %i: %i\n",i,thread[i]-&gt;thread_num);
     }
     pthread_exit(NULL);
     return 0;
  }
  </code></pre>
  
  <p>Any thoughts?</p>