Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>&gt; However, in pure C, this still gives a warning, and I don't understand why</p> <p>You've already identified the problem -- this code is not const-correct. "Const correct" means that, except for const_cast and C-style casts removing const, you can never modify a const object through those const pointers or references.</p> <p>The value of const-correctness -- const is there, in large part, to detect programmer errors. If you declare something as const, you're stating that you don't think it should be modified -- or at least, those with access to the const version only should not be able to modifying it. Consider:</p> <pre><code>void foo(const int*); </code></pre> <p>As declared, foo doesn't have <em>permission</em> to modify the integer pointed to by its argument. </p> <p>If you're not sure why the code you posted isn't const-correct, consider the following code, only slightly different from HappyDude's code:</p> <pre><code>char *y; char **a = &amp;y; // a points to y const char **b = a; // now b also points to y // const protection has been violated, because: const char x = 42; // x must never be modified *b = &amp;x; // the type of *b is const char *, so set it // with &amp;x which is const char* .. // .. so y is set to &amp;x... oops; *y = 43; // y == &amp;x... so attempting to modify const // variable. oops! undefined behavior! cout &lt;&lt; x &lt;&lt; endl; </code></pre> <p>Non-const types can only convert to const types in particular ways to prevent any circumvention of 'const' on a data-type without an explicit cast. </p> <p>Objects initially declared const are particularly special -- the compiler can assume they never change. However, if 'b' can be assigned the value of 'a' without a cast, then you could inadvertantly attempt to modify a const variable. This would not only break the check you asked the compiler to make, to disallow you from changing that variables value -- it would also allow you break the compiler optimizations!</p> <p>On some compilers, this will print '42', on some '43', and others, the program will crash. </p> <p><strong>Edit-add:</strong></p> <p>HappyDude: Your comment is spot on. Either the C langauge, or the C compiler you're using, treats const char * const * fundamentally differently than the C++ language treats it. Perhaps consider silencing the compiler warning for this source line only.</p> <p><strong>Edit-delete:</strong> removed typo</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload