title: const char* vs char* (C++)
body: <p>For the following program:</p>
  
  <pre><code>int DivZero(int, int, int);
  
  int main()
  {
      try {
          cout &lt;&lt; DivZero(1,0,2) &lt;&lt; endl;
      }
      catch(char* e)
      {
          cout &lt;&lt; "Exception is thrown!" &lt;&lt; endl;
          cout &lt;&lt; e &lt;&lt; endl;
          return 1;
      }
      return 0;
  }
  
  int DivZero(int a, int b, int c)
  {
      if( a &lt;= 0 || b &lt;= 0 || c &lt;= 0)
          throw "All parameters must be greater than 0.";
      return b/c + a;
  }
  </code></pre>
  
  <p>Using char* e will give </p>
  
  <blockquote>
    <p>terminate called after throwing an
    instance of 'char const*'</p>
  </blockquote>
  
  <p>According to <a href="http://www.programmingforums.org/thread30610.html" rel="nofollow noreferrer">C++ Exception Handling</a> , the solution is to use <strong>const char*</strong> instead.</p>
  
  <p>Further reading from <a href="http://bytes.com/topic/c/answers/222695-function-const-char-vs-function-char#post911541" rel="nofollow noreferrer">function (const char *) vs. function (char *)</a> said that</p>
  
  <blockquote>
    <p>The type of "String" is <code>char*', not
    </code>const char*'</p>
    
    <blockquote>
      <p>(this is a C discussion I think...)</p>
    </blockquote>
  </blockquote>
  
  <p>Additional reading on Stack Overflow <a href="https://stackoverflow.com/questions/3179645/char-vs-const-char-as-a-parameter">char* vs const char* as a parameter</a> tells me the difference. But none of them address my questions:</p>
  
  <ol>
  <li>It seems like both <strong>char*</strong> and <strong>string*</strong> have limit on the numbers of characters. Am I correct?</li>
  <li>How does adding the keyword <em>const</em> to <strong>char*</strong> eliminates that limit? I thought the only purpose of <strong>const</strong> is to set a flag that said "unmodifiable". I understand that <strong>const char*</strong> e means " the pointer which points to unmodifiable char type".</li>
  </ol>
  
  <p>The solution to that error is to use <strong><em>const</em> char* e</strong>.</p>
  
  <p>Even const string* e doesn't work. (just for the sake of testing...)</p>
  
  <p>Can anyone explain, please? Thank you!</p>
  
  <p>By the way, I am on Ubuntu, compiled by GCC, on Eclipse.</p>