title: How to cast from const char* to a struct?
body: <p>During the development of a Qt application for Embedded Linux (where improvements in performance are really welcomed), I came across the necessity to cast from a array of chars to a given struct. Till now, that was being done with the code:</p>
  
  <pre><code>MyStruct* const my_struct = reinterpret_cast&lt; MyStruct* &gt;(qbytearray.data());
  </code></pre>
  
  <p>while <code>data()</code> being a member of the Qt class <code>QByteArray</code> that transforms the byte array to a <code>char*</code>. In doing this, tough, it makes a deep copy of the data, which is not good given the extra processing. Since I want to only read the data (the struct casted will never be used to edit), the alternative method <code>QByteArray::constData()</code> is preferable, since it doesn't make a deep copy, but in contrast returns a <code>const char*</code> instead of <code>char*</code>.</p>
  
  <p>The question is: how I should do the casting now? I tried to use const_cast without success.</p>
  
  <pre><code>MyStruct* const my_struct = const_cast&lt; MyStruct* &gt;(qbytearray.constData()); // compile error
  
  const MyStruct* const my_struct = const_cast&lt; MyStruct* &gt;(qbytearray.constData()); // compile error
  </code></pre>
  
  <p>and <code>reinterpret_cast</code> also didn't work because "it casts away the qualifiers", which is expected. The closest way I was capable of doing this was by first casting to <code>char*</code> and later to the struct:</p>
  
  <pre><code>MyStruct* const my_struct = (MyStruct*)const_cast&lt; char* &gt;(qbytearray.constData()); 
  </code></pre>
  
  <p>but I get the feeling that not only this is "circle around" the problem, but also that the casting from <code>char*</code> to <code>MyStruct*</code> will ultimately sacrifice the processing improvement I was desiring.</p>
  
  <p>So how do this casting correctly?</p>