title: Pointer casting problem with struct array member
body: <p>I've run across this source in a legacy code base and I don't really know why exactly it behaves the way it does.</p>
  
  <p>In the following code, the <code>pData</code> struct member either contains the data or a pointer to the real data in shared memory. The message is sent using IPC (<code>msgsnd()</code> and <code>msgrcv()</code>). Using the pointer casts (that are currently commented out), it fails using GCC 4.4.1 on an ARM target, the member <code>uLen</code> gets modified. When using <code>memcpy()</code> and everything works as expected. I can't really see what is wrong with the pointer casting. What is wrong here?</p>
  
  <pre><code>typedef struct {
      long mtype;
      unsigned short uRespQueue;
      unsigned short uID;
      unsigned short uLen;
      unsigned char pData[8000];
  } message_t;
  
  // changing the pointer in the struct
  {
      unsigned char *pData = &lt;some_pointer&gt;;
  #if 0
      *((unsigned int *)pMessage-&gt;pData) = (unsigned int)pData;
  #else
      memcpy(pMessage-&gt;pData, &amp;pData, sizeof(unsigned int));
  #endif
  }
  
  // getting the pointer out
  {
  #if 0
      unsigned char *pData; (unsigned char *)(*((unsigned int *)pMessage-&gt;pData));
  #else
      unsigned char *pData;
      memcpy(&amp;pData, pMessage-&gt;pData, sizeof(int));
  #endif
  }
  </code></pre>