<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Cygwin (vers 1st September 2004), see www.w3.org">
  <meta http-equiv="Content-Type" content=
  "text/html; charset=us-ascii">

  <title>RCST7 - C204</title>
  <link rel="stylesheet" type="text/css" href="errors.css">
</head>

<body>
  <h1><a name="RCST7-C204" id="RCST7-C204"></a>RCST7 ERROR
  C204</h1><!-- Note that the header should be non scrollable, so that it always
     remains on top even if the user scrolls down a loooong text. -->
  <!-- This is the Error title -->

  <h2>pointer: const to non-const assignment</h2>
  <!--Footer Start-->

  <p>An attempt was make to assign a const pointer to a non-const.
  This usually happens when you use const-qualified pointers (in
  order to enforce your code's safety) and pass the pointer to a
  function expecting a non-const pointer.</p>

  <p>The C language forbids assignment of a const pointer to a
  non-const.</p>

  <h2>Example</h2>
  <pre>
const char * constptr = "Hello";

void foo(char *ptr)
{
   // Some code here
}

void main(void)
{
   foo(constptr);   // Error 204: const to non-const assignment
}
  
</pre>

  <h2>Resolution</h2>

  <p>Different approaches can be taken to solve this problem:</p>

  <ul>
    <li>Make your pointer non-const. this has the undesirable
    effect of weakening your code.</li>

    <li>Cast your pointer to non-const upon assignment. For
    example, in the code fragment above, replace the call to foo()
    by:
    <pre>foo((char*)constptr);   // OK</pre>
    </li>

    <li>If the function that processes you pointer does not
    functionally modify your pointer, you can qualify the pointer
    argument as a const pointer. This improves your code quality,
    as your C compiler will ensure that you do not perform any
    potential modification to your pointer in the function's code.
    For example, in the code fragment above, replace the
    declaration of foo() by:
      <pre>
void foo(const char *ptr)
</pre>
    </li>
  </ul>

  <div class="footer">
    <hr>
    <!--Copyright-->

    <p>&copy; Raisonance S.A.S. 1987-2007. All rights reserved.</p>
  </div><!--Closes the footer div-->
  <!--FOOTER_END-->
</body>
</html>
