title: Pointers difference between C and C++
body: <p>I've noted that the following C code give "warning: initialization discards qualifiers from pointer target type" but it still compiles and behave as expected (outputting 'W' char).</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main(int argc, char *argv[])
  {
      char buffer[20] = {'H','e','l','l','o',' ','W','o','r','l','d','!','\0'};
  
      const char* p = &amp;buffer[0];
  
      char* c = (p + 6);
  
      printf("%c\n",*c);
  }
  </code></pre>
  
  <p>In C++ rather similar code doesn't compile at all complaining about "error: invalid conversion from ‘const char*’ to ‘char*’ "</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  int main()
  {
     char buffer[20] = {'H','e','l','l','o',' ','W','o','r','l','d','!','\0'};
  
     const char* p = &amp;buffer[0];
  
     char* c = p + 6;
  
     cout &lt;&lt; *c;
     cout &lt;&lt; endl;
     return 0;
  }
  </code></pre>
  
  <p>What is the reason?</p>
  
  <p>Is it possible to fix the C++ code to make it compile (and behave) like its C counterpart?</p>
  
  <p>Better explanation: Thanks for all of your answers, but most of you didn't get my real problem so I'll try to explain in more details.</p>
  
  <p>I'm using a library written in C. A function prototype in the header is something like:</p>
  
  <pre><code>void parse (const char* p, uint16_t len, uint8_t is_eof);
  </code></pre>
  
  <p>Inside the implementation of this function it happens to run code like</p>
  
  <pre><code>char* c = p + 6;
  </code></pre>
  
  <p>Everything is fine if I write my code in C and compile against this library.</p>
  
  <p>Now I want to port this library in C++ since I need it in a C++ project. I successfully ported the library by redefining the function parameter as a variable rather than a constant. But since the <code>p</code> pointer actually need NOT to change I'd prefer to define it as a constant, as is in the original C lib. Putting a variable rather than a constant I end up porting a library without preserving all the original semantics. This is bad since I can't know if the C++ library runs as well as the original C one.</p>
  
  <p>I hope this is clear enough.</p>
  
  <p>A (hopefully) even better explanation:</p>
  
  <p>I'm following the tutorial in <a href="http://www.avrfreaks.net/index.php?name=PNphpBB2&amp;file=printview&amp;t=80042&amp;start=0" rel="nofollow">AVR Tutorials - [CODE][C] Parsing strings flexibly/efficiently with Ragel</a> and the issue is relative to the <code>parse_microscript()</code> function.</p>
  
  <p>Since I'd like to embed Arduino code (that is, <code>Serial.println();</code>) in the parser, I'm trying to compile this Ragel parser in a C++ host language, rather than C as proposed in the tutorial.</p>
  
  <p>First, I try to include <code>Arduino.h</code> in the generated C code, but the Arduino <a href="http://en.wikipedia.org/wiki/Integrated_development_environment" rel="nofollow">IDE</a> won't compile (I believe because of mixing C and C++ code).</p>
  
  <p>I then tried to generate C++ code from the same Ragel script, but when compiling multiple "invalid conversion from ‘const char*’ to ‘char*’" errors ar fired, targeting the generated code.</p>
  
  <p>The only means to make it work in C++ had been to remove all constant keywords. This is bad since I'm changing the original semantic of the C code, but it works.</p>
  
  <p>I tried to isolate the problem providing the above C and C++ snippets, but I probably poorly explained the point.</p>
  
  <p>The accepted answer is what let the C++ snippet compile, but when I try this solution in Arduino code it doesn't work.</p>
  
  <p>I put the working Ragel script compiled with the command <code>ragel -G2 -o microscript.cpp microscript.rl</code>:</p>
  
  <pre><code>#include "qp_port.h"
  #include "pelican.h"
  #include "bsp.h"
  #include "Arduino.h"
  
  static uint16_t currentNumber;
  
  %%{
      machine microscript;
  
      action ClearNumber {
          currentNumber = 0;
      }
  
      action PrintNumber {
          Serial.print("parameter: ");
          Serial.println(currentNumber);
      }
  
      action RecordDigit {
          uint8_t digit = (*p) - '0';
          currentNumber = (currentNumber * 10) + digit;
      }
  
      number = ((digit @RecordDigit)+) &gt; ClearNumber % PrintNumber;
      whitespace = space+;
  
      numlist = number (',' number)*;
  
      crlf = '\r\n';
  
      OK = crlf 'OK' crlf;
  
      main := |*
  
      crlf =&gt; {Serial.println("crlf");};
  
      OK =&gt; {Serial.println("OK detected");};
  
      *|;
  }%%
  
  %% Write data;
  
  static uint8_t cs; /* The current parser state */
  static char* ts;
  static char* te;
  static char* act;
  
  
  void init_microscript() {
      %% write init;
  }
  
  void parse_microscript(char* p, uint16_t len, uint8_t is_eof) {
      char* pe = p + len; /* pe points to 1 byte beyond the end of this block of data */
      char* eof = is_eof ? pe : ((char*) 0); /* Indicates the end of all data, 0 if not in this block */
  
      %% write exec;
  }
  </code></pre>
  
  <p>As you can see too, I had to change the first parameter of the <code>parse_microscript</code> function from const <code>char* p</code> to <code>char*p</code> , and <code>const char* pe</code> to <code>char* pe</code>.</p>