title: Storing a pointer's address in an unsigned int in C
body: <p>Is it possible to cast a pointer to an unsigned int, then later cast it back to a pointer?  I'm trying to store the pointer to a struct in a pthread_t variable, but I can't seem to get it to work.  Here's some snippets of my code (I'm creating a user-level thread management library).  When I try to print out the tid of the thread it gives me some long garbage number.</p>
  
  <p>Edit: Never mind, I got it to work.</p>
  
  <p>I changed</p>
  
  <pre><code>thread = (pthread_t) currentThread;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>*thread = (pthread_t) currentThread;
  </code></pre>
  
  <p>Figured it was something stupid like that.</p>
  
  <hr>
  
  <p>Test program:</p>
  
  <pre><code>pthread_t thread1;
  pthread_t thread2;
  
  pthread_create(&amp;thread1, NULL, runner, NULL);
  pthread_create(&amp;thread2, NULL, runner, NULL);
  pthread_join(&amp;thread2, NULL);
  </code></pre>
  
  <p>My library:</p>
  
  <pre><code>typedef struct queueItem
  {
      int tid;
      ucontext_t context;
  
      int caller;
  
      struct queueItem *joiningOn;
      struct queueItem *nextContext;
  } queueItem;
  
  int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
  {
      thread = (pthread_t) currentThread;
  }
  
  ...
  
  int pthread_join(pthread_t thread, void **retval)
  {
      queueItem *t = (queueItem *) thread;
  
      if(runningContext-&gt;joiningOn != NULL) // Current thread is already waiting on another
          return EINVAL;
      if(t == NULL) // If thread to join on is invalid
          return 0;
  
      fprintf(stdout, "JOINEE: %d\n", t-&gt;tid); // Prints weird number
  
      runningContext-&gt;caller = JOIN;
      runningContext-&gt;joiningOn = t;
      swapcontext(&amp;(runningContext-&gt;context), &amp;scheduleContext);
  }
  </code></pre>