title: Casting to a struct from LPVOID - C
body: <p>I am writing a simple console application which will allow me to create a number of threads from a set of parameters passed through the arguments I provide.</p>
  
  <pre><code>DWORD WINAPI ThreadFunc(LPVOID threadData)
  {
  }
  </code></pre>
  
  <p>I am packing them into a struct and passing them as a parameter into the CreateThread method and trying to unpack them by casting them to the same type as my struct from the LPVOID.</p>
  
  <p>I'm not sure how to cast it to the struct after getting it through so I can use it in the method itself, i've tried various combinations (Example attatched) but it won't compile.</p>
  
  <p>Struct:</p>
  
  <pre><code>#define numThreads 1
  
  struct Data
  {
      int threads;
      int delay;
      int messages;
  };
  </code></pre>
  
  <p>Call to method:</p>
  
  <pre><code>HANDLE hThread;
      DWORD threadId;
      struct Data *tData;
  
      tData-&gt;threads = numThreads;
      tData-&gt;messages = 3;
      tData-&gt;delay = 1000;
  
  
      // Create child thread
      hThread = CreateThread(
                              NULL,       // lpThreadAttributes (default)
                              0,          // dwStackSize (default)
                              ThreadFunc, // lpStartAddress
                              &amp;tData,     // lpParameter
                              0,          // dwCreationFlags
                              &amp;threadId   // lpThreadId (returned by function)
                             );
  </code></pre>
  
  <p>My attempt:</p>
  
  <pre><code>DWORD WINAPI ThreadFunc(LPVOID threadData)
      {
          struct Data tData = (struct Data)threadData;
  
          int msg;
  
          for(msg = 0; msg&lt;5; msg++)
          {
              printf("Message %d from child\n", msg);
          }
          return 0;
  }
  </code></pre>
  
  <p>Compiler error:</p>
  
  <p>error C2440: 'type cast' : cannot convert from 'LPVOID' to 'Data'</p>
  
  <p>As you can see I have implemented a way to loop through a number of messages already, I'm trying to make things slightly more advanced and add some further functionality.</p>