title: Multiple arguments to function called by pthread_create()?
body: <p>I need to pass multiple arguments to a function that I would like to call on a separate thread. I've <a href="https://computing.llnl.gov/tutorials/pthreads/#PassingArguments" rel="noreferrer">read</a> that the typical way to do this is to define a struct, pass the function a pointer to that, and dereference it for the arguments. However, I am unable to get this to work:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  
  struct arg_struct {
      int arg1;
      int arg2;
  };
  
  void *print_the_arguments(void *arguments)
  {
      struct arg_struct *args = (struct arg_struct *)args;
      printf("%d\n", args -&gt; arg1);
      printf("%d\n", args -&gt; arg2);
      pthread_exit(NULL);
      return NULL;
  }
  
  int main()
  {
      pthread_t some_thread;
      struct arg_struct args;
      args.arg1 = 5;
      args.arg2 = 7;
  
      if (pthread_create(&amp;some_thread, NULL, &amp;print_the_arguments, (void *)&amp;args) != 0) {
          printf("Uh-oh!\n");
          return -1;
      }
  
      return pthread_join(some_thread, NULL); /* Wait until thread is finished */
  }
  </code></pre>
  
  <p>The output for this should be:</p>
  
  <pre><code>5
  7
  </code></pre>
  
  <p>But when I run it I actually get:</p>
  
  <pre><code>141921115
  -1947974263
  </code></pre>
  
  <p>Anyone know what I'm doing wrong?</p>