Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Without a pointer, you would have</p> <pre><code>const Dict currPtr </code></pre> <p>which is a constant Dict. Now, if you make it a pointer you have</p> <pre><code>const Dict *currPtr </code></pre> <p>which is a pointer to a constant Dict. That does not mean that the pointer is constant. But it does mean that the Dict pointed to is treated as constant</p> <pre><code>currPtr-&gt;key = 10; // error, dict is treated as constant. </code></pre> <p>But the pointer is not</p> <pre><code>currPtr = otherPtr; // possible: the pointer is not constant </code></pre> <p>You would get an error for the second case if you make the pointer constant. Keeping the pointed dict constant too, this would look like this</p> <pre><code>const Dict * const currPtr = init; </code></pre> <p>Now you can't set currPtr to point to something different, because the pointer is now constant, not just what the pointer points to is treated so. Some people like the look if the const is always right to the stuff that it makes const. This would look like this</p> <pre><code>Dict const * const currPtr = init; </code></pre> <p>which is the same as the previous snippet. If you then read it from right to left, it tells you what it is "const pointer to a const Dict". If you have a type, it doesn't matter how you order the specifiers</p> <pre><code>int const a = 10; const int b = 10; </code></pre> <p>Both are constant integers. That is why we could put const right of the Dict type specifier.</p> <p>Now, if you have a pointer, you can always pretend you point to a constant object, even though the object wasn't declared const. But you can't pretend to work with a non-const object if what you point to is a const object:</p> <pre><code>int const *p = NULL; // doesn't work without a cast. Will at least provoke a warning int *pn = p; int *p = NULL; // always works: pretending to point to something const doesn't harm. int const *pc = p; </code></pre> <p>Note that if you make the pointer itself const, the rules are different to that. They are analogous to const applied to other types:</p> <pre><code>int const i = 0; int j = i; // works. we only read the value of i. its const doesn't matter. int * const p = NULL; int * q = p; // works: we only read the value of p (a null pointer). </code></pre> <p>After copying the value into a new variable (whether pointer or not), the new variable is not connected in any way to the other variable, because the value read has no associativity to how the value was created in the first place. The const of the other variable doesn't matter. </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. 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.
    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