Note that there are some explanatory texts on larger screens.

plurals
  1. POEasy rule to read complicated const declarations?
    text
    copied!<p>For reading complex pointer declarations there is the <a href="http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html" rel="noreferrer">right-left rule</a>.</p> <p>But this rule does not mention how to read <code>const</code> modifiers.</p> <p>For example in a simple pointer declaration, <code>const</code> can be applied in several ways:</p> <pre><code>char *buffer; // non-const pointer to non-const memory const char *buffer; // non-const pointer to const memory char const *buffer; // equivalent to previous declartion char * const buffer = {0}; // const pointer to non-const memory char * buffer const = {0}; // error const char * const buffer = {0}; // const pointer to const memory </code></pre> <p>Now what about the use of <code>const</code> with a pointer of pointer declaration?</p> <pre><code>char **x; // no const; const char **x; char * const *x; char * * const x; const char * const * x; const char * * const x; const char * const * const x; </code></pre> <p>And what is an easy rule to read those declarations? Which declarations make sense? </p> <p>Is the <a href="http://c-faq.com/decl/spiral.anderson.html" rel="noreferrer">Clockwise/Spiral Rule</a> applicable?</p> <h2>Two real world examples</h2> <p>The method <a href="http://clang.llvm.org/doxygen/classclang_1_1ASTUnit.html#ab18e63a754071c368a0afccbec0de8a8" rel="noreferrer"><code>ASTUnit::LoadFromCommandLine</code></a> uses <code>const char **</code> to supply command line arguments (in the llvm clang source).</p> <p>The argument vector parameter of <a href="http://linux.die.net/man/3/getopt" rel="noreferrer"><code>getopt()</code></a> is declared like this:</p> <pre><code>int getopt(int argc, char * const argv[], const char *optstring); </code></pre> <p>Where <code>char * const argv[]</code> is equivalent to <code>char * const * argv</code> in that context.</p> <p>Since both functions use the same concept (a vector of pointers to strings to supply the arguments) and the declarations differ - the obvious questions are: Why do they differ? Makes one more sense than the other?</p> <p>The intend should be: The <code>const</code> modifier should specify that the function does not manipulate strings of this vector and does not change the structure of the vector.</p>
 

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