title: Casting const void pointer to array of const char pointers properly in C
body: <p>I have a piece of C code that looks like this:</p>
  
  <pre><code>const char (*foo)[2] = bar();
  </code></pre>
  
  <p>Now <code>bar()</code> is a function that returns a <code>(const void *)</code>. How do I properly cast this <code>const</code> pointer? The code produces this warning from GCC :   </p>
  
  <pre><code>"initialization discards qualifiers from pointer target type".   
  </code></pre>
  
  <p>Here are some of my unsuccessful attempts:</p>
  
  <pre><code>const char (*foo)[2] = (const char *)bar();
  const char (*foo)[2] = (const void **)bar();
  </code></pre>
  
  <p>The original code does work, I just can't get rid of the warnings by properly casting the return value.</p>
  
  <p>EDIT : This has been suggested:</p>
  
  <pre><code>const char (*foo)[2] = (const char (*)[2])bar();
  </code></pre>
  
  <p>It appears to be correct, but GCC gives this warning :   </p>
  
  <pre><code>"cast discards qualifiers from pointer target type"   
  </code></pre>
  
  <p>which is nearly identical to the original warning.</p>
  
  <p>EDIT 2 : OK, I think I've got it. The real problem here is the <code>( const void * )</code> definition of <code>bar()</code>. The <code>const</code> in the definition <code>(const char( * )[2])</code> refers to the elements of the array, not the pointer to the array. This type definition is essentially an array, which when represented by a <code>void</code> pointer is <em>not</em> <code>const</code>. The real answer is that a <code>( const void * )</code> loses its <code>cons</code>t-ness when cast to <code>(const char ( * )[2])</code>.     </p>