title: Why casting long can solve "warning: cast to pointer from integer of different size"?
body: <p>I am practicing concurrent programming. My ultimate goal is applying the multiple thread on OpenCV and 3rd party software. I am reading the following URL.<br>
  <code>http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm</code></p>
  
  <pre><code>      rc = pthread_create(&amp;threads[i], NULL, 
                            PrintHello, (void *)i);
  </code></pre>
  
  <p>It returns me an error.</p>
  
  <pre><code>$ g++ simpleThread.cpp -lpthread
  simpleThread.cpp: In function ‘int main()’:
  simpleThread.cpp:25:47: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
                             PrintHello, (void *)i);
  </code></pre>
  
  <p>Then I read <code>http://stackoverflow.com/questions/21323628/warning-cast-to-from-pointer-from-to-integer-of-different-size</code>
  Add <code>long</code></p>
  
  <pre><code>  rc = pthread_create(&amp;threads[i], NULL, 
                        PrintHello, (void *)(long)i);
  </code></pre>
  
  <p>Program works.<br><br></p>
  
  <p><strong>Question :</strong><br>
  I don't understand at all. Why I have to put <code>long</code> in here?<br>
  Where, what topic, book, URLs I can start?<br>
  <br>
  <strong>Update :</strong> <br>
  Thank you everyone for your attention.
  Casting by <code>long</code> returns me acceptable result.<br></p>
  
  <pre><code>rc = pthread_create(&amp;threads[i], NULL, 
                            PrintHello, (void*)(long)i);
  </code></pre>
  
  <p>Run the program.</p>
  
  <pre><code>$./simpleThreadLong.out 
  main() : creating thread, 0
  main() : creating thread, 1
  Hello World! Thread ID, 0
  main() : creating thread, Hello World! Thread ID, 2
  1
  main() : creating thread, 3
  Hello World! Thread ID, 2
  main() : creating thread, 4
  Hello World! Thread ID, 3
  Hello World! Thread ID, 4
  </code></pre>
  
  <p>Whereas ampersand returns unknown ID.<br></p>
  
  <pre><code>  rc = pthread_create(&amp;threads[i], NULL, 
                        PrintHello, &amp;i);
  </code></pre>
  
  <p>Run the program.</p>
  
  <pre><code>./simpleThreadAmpersand.out 
  main() : creating thread, 0
  main() : creating thread, 1
  main() : creating thread, 2
  Hello World! Thread ID, 140722119501784
  Hello World! Thread ID, 140722119501784
  Hello World! Thread ID, 140722119501784
  main() : creating thread, 3
  main() : creating thread, 4
  Hello World! Thread ID, 140722119501784
  Hello World! Thread ID, 140722119501784
  </code></pre>
  
  <p><strong>Update2:</strong> <br></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;pthread.h&gt;
  
  using namespace std;
  
  #define NUM_THREADS     5
  
  void *PrintHello(void *threadid)
  {
     long tid;
     tid = (long)threadid;
     cout &lt;&lt; "Hello World! Thread ID, " &lt;&lt; tid &lt;&lt; endl;
     pthread_exit(NULL);
  }
  
  int main ()
  {
     pthread_t threads[NUM_THREADS];
     int rc;
     int i;
     for( i=0; i &lt; NUM_THREADS; i++ ){
        cout &lt;&lt; "main() : creating thread, " &lt;&lt; i &lt;&lt; endl;
        //rc = pthread_create(&amp;threads[i], NULL, 
        //                    PrintHello, (void *)(long)i);
        rc = pthread_create(&amp;threads[i], NULL, 
                            PrintHello, &amp;i);
        if (rc){
           cout &lt;&lt; "Error:unable to create thread," &lt;&lt; rc &lt;&lt; endl;
           exit(-1);
        }
     }
     pthread_exit(NULL);
  }
  </code></pre>