title: const correctness and parameters to functions with structs containing void*
body: <p>I'm not going to say I <em>fully</em> understand the idea of const correctness but let's at least say that I understand it.  So, when I encountered this, I was/am stumped.  Can someone please explain this to me.  Consider the following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  struct Test {
      int someInt;
      void * pSomething;
  };
  
  void TestFunc(Test * const pStruct) {
      pStruct-&gt;someInt = 44;
      pStruct-&gt;pSomething = "This is a test string"; // compiler error here
  }
  
  int main() {
      Test t;
      TestFunc(&amp;t);
      return 0;
  }
  </code></pre>
  
  <p>At the point I've annotated with a comment I get this error from gcc (4.5.3 for cygwin):</p>
  
  <pre><code>foo.cpp:10:24: error: invalid conversion from `const void*' to `void*'
  </code></pre>
  
  <p>It's apparently something to do with the fact that the struct contains a void* because some experimentation revealed that changing the struct to:</p>
  
  <pre><code>struct Test {
      int someInt;
      char * pSomething;
  };
  </code></pre>
  
  <p>produces a warning as opposed to an error.  Also, leaving the structure unchanged but modifying this line to include the following cast compiles the code without warning:</p>
  
  <pre><code>pStruct-&gt;pSomething = (void*)"This is a test string"; // no error now
  </code></pre>
  
  <p>What I don't understand, given my understanding of <em>const correctness</em>, is why is the compiler emitting this error, "<em>invalid conversion from ‘const void*’ to ‘void*’</em>" at all?  Since the const modifier on the function definition makes it so that the pointer is constant but what it points to is not, why should this be a problem?  I'm assuming that there is some sort of implicit cast happening, as originally written, since the string literal would be something like <code>const char *</code> that must be converted to <code>void *</code>.  That notwithstanding, the question remains since what <code>pStruct</code> points to is <strong>not</strong> constant.</p>
  
  <p>For reference, I read up on <a href="http://www.parashift.com/c++-faq/overview-const.html" rel="nofollow">const correctness here</a> and <a href="http://www.parashift.com/c++-faq/const-ptr-vs-ptr-const.html" rel="nofollow">here</a>.</p>