    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Reading C &amp; C++ Variable Declarations</title>
        <link>https://members.accu.org/index.php/articles/1035</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 12, #4 - Jul 2000</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c125/">124</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-125/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+125/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Reading C &amp; C++ Variable Declarations</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 July 2000 13:15:38 +01:00 or Mon, 03 July 2000 13:15:38 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>I was never formally taught the C language. In college in 1979
the main teaching language was Pascal. I learnt C by looking over
the shoulders of more knowledgeable colleagues in my first job. By
studying their program listings it was fairly obvious that
<tt class="literal">int i;</tt> declared that <tt class=
"varname">i</tt> was an integer, that <tt class="literal">char
*cp;</tt> declared a pointer to a character and that <tt class=
"literal">char buf[100];</tt> declared an array of 100 characters.
I soon came to understand that <tt class="literal">char
*argv[]</tt> was an array of pointers to characters. But that was
about as far as I got. I found more complex declarations puzzling.
I was reminded of this recently while reading an interview with
Bjarne Stroustrup [<a href=
"#Stroustrup2000">Stroustrup2000</a>]:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>[Interviewer:] In another interview, you defined the C
declarator syntax as an experiment that failed. However, this
syntactic construct has been around for 27 years and perhaps more;
why do you consider it problematic (except for its cumbersome
syntax)?</p>
<p>[Stroustrup's reply:] I don't consider it problematic except for
its cumbersome syntax. It is good and necessary to be able to
express ideas such as &quot;p is a pointer to an array of 10 elements
that are pointers to functions taking two integer arguments and
returning a bool.&quot; However,</p>
<pre class="programlisting">
bool (*(*p)[10])(int,int); 
</pre>
<p>is not an obvious way of saying that. In real life, I'd have to
use a typedef to get it right:</p>
<pre class="programlisting">
typedef bool (*Comparison)(int,int); 
Comparison (*p)[10]; 
</pre>
<p>&hellip;</p>
</blockquote>
</div>
<p>I find the fact that Stroustrup finds declarations difficult to
get right somewhat reassuring. I, too, use his technique of
breaking complex declarations down into more manageable stages
using typedefs. I think his example is a good one: I can grasp the
declaration of p using typdefs much quicker than I can understand
the one line declaration. And I could not have understood the
latter at all before I learnt the &quot;Right-Left&quot; rule, more on which
in a moment.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e57" id="d0e57"></a>Back to
Basics</h2>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<p>&quot;Declarations specify the interpretation given to each
identifier&hellip;&quot;</p>
</blockquote>
</div>
<p>If you want to really understand C declarations I suggest the
best place look is section A8 of <i class="citetitle">The C
Programming Language</i> [<a href="#KandR1988">KandR1988</a>], from
which the above quote is taken. There you will find a dozen pages
describing the syntax and meaning of declarations. For a concise
summary I would turn to section 4.9.1 of <i class="citetitle">The
C++ Programming Language</i> [<a href=
"#Stroustrup1997">Stroustrup1997</a>], from which the following
extract is taken:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>A declaration consists of four parts: an optional &quot;specifier,&quot; a
base type, a declarator, and an optional initializer. Except for
function and namespace definitions, a declaration is terminated by
a semicolon. For example:</p>
<pre class="programlisting">
  char* kings[] = 
      {&quot;Antigonus&quot;, &quot;Seleucus&quot;, &quot;Ptolemy&quot;};
</pre></blockquote>
</div>
<p>Here, the base type is <span class=
"emphasis"><em>char</em></span>, the declarator is <span class=
"emphasis"><em>*kings[]</em></span>, and the initializer is
=<span class="emphasis"><em>{&hellip;}</em></span>.</p>
<p>A specifier is an initial keyword, such as <tt class=
"literal">virtual</tt> and <tt class="literal">extern</tt>, that
specifies some non-type attribute of what is being declared.</p>
<p>A declarator is composed of a name and optionally some
declarator operators. The most common declarator operators are:</p>
<div class="informaltable">
<table border="0">
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody&gt;
<tr>
<td>*</td>
<td>pointer</td>
<td>prefix</td>
</tr>
<tr>
<td>*const</td>
<td>constant pointer</td>
<td>prefix</td>
</tr>
<tr>
<td>&amp;</td>
<td>reference</td>
<td>prefix</td>
</tr>
<tr>
<td>[]</td>
<td>array</td>
<td>postfix</td>
</tr>
<tr>
<td>()</td>
<td>function</td>
<td>postfix</td>
</tr>
&lt;/tbody&gt;
</table>
</div>
<p>Their use would be simple if they were all either prefix or
postfix. However, <tt class="literal">*</tt>, <tt class=
"literal">[]</tt>, and <tt class="literal">()</tt> were designed to
mirror their use in expressions. Thus, <tt class="literal">*</tt>
is prefix and <tt class="literal">[]</tt> and <tt class=
"literal">()</tt> are postfix. The postfix declarator operators
bind tighter than the prefix ones. Consequently, <tt class=
"literal">*kings[]</tt> is a vector of pointers to something, and
we have to use parentheses to express types such as &quot;pointer to
function.&quot;</p>
<p>So to turn Stroustrup's kings declarator example from a vector
(array) of pointers to something into a pointer to a vector of
something we would use parentheses like so: <tt class=
"literal">(*kings)[]</tt>. It is this mixture of pre- and postfix
operators where the Right-Left rule can help.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e170" id="d0e170"></a>The
Right-Left Rule</h2>
</div>
<p>The rule is described in various places on the web, which is
where I first came across it. But I do not know who first coined
the term. The Right-Left rule for reading declarations is:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Start with the identifier. Say, &quot;identifier is.&quot;</p>
</li>
<li>
<p>Go right, interpreting the operators you find according to the
table below. If you encounter a right parenthesis, or there are no
more operators, go left from the identifier.</p>
</li>
<li>
<p>When going left interpret the operators according to the table
below. If you encounter the base type just say it. If you encounter
a left parenthesis, or there are no more operators, go right from
where you stopped going right last time.</p>
</li>
</ol>
</div>
<p>Repeat rules 2 and 3 until there are no more operators to
interpret.</p>
<div class="informaltable">
<table border="0">
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody&gt;
<tr>
<td>*</td>
<td>&quot;pointer to&quot;</td>
</tr>
<tr>
<td>&amp;</td>
<td>&quot;reference to&quot;</td>
</tr>
<tr>
<td>[]</td>
<td>&quot;array of&quot;</td>
</tr>
<tr>
<td>[n]</td>
<td>&quot;array of n&quot;</td>
</tr>
<tr>
<td>()</td>
<td>&quot;function returning&quot;</td>
</tr>
<tr>
<td>(arg)</td>
<td>&quot;function taking arg and returning&quot;</td>
</tr>
&lt;/tbody&gt;
</table>
</div>
<p>Using this rule to work through the example Stroustrup quotes in
the above interview:</p>
<pre class="screen">
bool (*(*<span class="bold"><b>p</b></span>)[10])(int, int);
</pre>
<p>Rule 1, locate the identifier: <span class="emphasis"><em>&quot;p
is&quot;</em></span></p>
<pre class="screen">
bool (*(*p<span class="bold"><b>)</b></span>[10])(int, int);
</pre>
<p>Rule 2, go right, encounter right parentheses, go left.</p>
<pre class="screen">
bool (*(<span class="bold"><b>*</b></span>p)[10])(int, int);
</pre>
<p>Rule 3, go left, encounter <span class="bold"><b>*</b></span>,
look up in table: <span class="emphasis"><em>&quot;p is pointer
to&quot;</em></span></p>
<pre class="screen">
bool (*<span class="bold"><b>(</b></span>*p)[10])(int, int);
</pre>
<p>Rule 3, go left, encounter left parentheses, go right.</p>
<pre class="screen">
bool (*(*p)<span class="bold"><b>[10]</b></span>)(int, int);
</pre>
<p>Rule 2, go right, encounter [10], look up in table: <span class=
"emphasis"><em>&quot;p is pointer to array of 10&quot;</em></span></p>
<pre class="screen">
bool (*(*p)[10]<span class="bold"><b>)</b></span>(int, int);
</pre>
<p>Rule 2, go right, encounter right parentheses, go left.</p>
<pre class="screen">
bool (<span class="bold"><b>*</b></span>(*p)[10])(int, int);
</pre>
<p>Rule 3, go left, encounter <span class="bold"><b>*</b></span>,
look up in table: <span class="emphasis"><em>&quot;p is pointer to array
of 10 pointers to&quot;</em></span></p>
<pre class="screen">
bool <span class="bold"><b>(</b></span>*(*p)[10])(int, int);
</pre>
<p>Rule 3, go left, encounter left parentheses, go right.</p>
<pre class="screen">
bool (*(*p)[10])<span class="bold"><b>(int, int)</b></span>;
</pre>
<p>Rule 2, go right, encounter <span class="bold"><b>(int,
int)</b></span>, look up in table: <span class="emphasis"><em>&quot;p is
pointer to array of 10 pointers to function taking args &quot;int, int&quot;
and returning&quot;</em></span></p>
<pre class="screen">
bool (*(*p)[10])<span class="bold"><b>(int, int);</b></span>
</pre>
<p>Rule 2, go right, no more operators, go left.</p>
<pre class="screen">
<span class="bold"><b>bool</b></span> (*(*p)[10])(int,int);
</pre>
<p>Rule 3, go left, and encounter base type: <span class=
"emphasis"><em>&quot;p is pointer to array of 10 pointers to function
taking args &quot;int, int&quot; and returning bool&quot;</em></span></p>
<p>As you can see, this simple mechanical process has produced a
fairly clear description of the given declaration. It is worth
noting that just because you can use the Right-Left rule to turn a
declaration into English, it isn't necessarily legal C. For
example, the Right-Left rule will merrily read</p>
<pre class="programlisting">
int (*fn)()[7];
</pre>
<p>as &quot;fn is a pointer to a function returning array of 7 ints,&quot;
which, as you know, is not permitted in C.</p>
<p>Not everyone will find this mechanical approach to reading
declarations necessary or useful. But personally I do. Some years
ago I saw a program that, given a C declaration, would spit out the
equivalent English description. I thought this was quite magic at
the time, but now I can see it would not be too hard to encode the
Right-Left rule.</p>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e337" id="d0e337"></a>References</h2>
</div>
<div class="bibliomixed"><a name="KandR1988" id="KandR1988"></a>
<p class="bibliomixed">[KandR1988] Brian Kernighan &amp; Dennis
Ritchie The C Programming Language Second Edition, Prentice Hall,
1988.</p>
</div>
<div class="bibliomixed"><a name="Stroustrup1997" id=
"Stroustrup1997"></a>
<p class="bibliomixed">[Stroustrup1997] Bjarne Stroustrup
<span class="citetitle"><i class="citetitle">The C++ Programming
Language</i></span> Third Edition, Addison Wesley, 1997.</p>
</div>
<div class="bibliomixed"><a name="Stroustrup2000" id=
"Stroustrup2000"></a>
<p class="bibliomixed">[Stroustrup2000] Interview in <span class=
"citetitle"><i class="citetitle">Visual C Developers
Journal</i></span> and reproduced at: <span class=
"bibliomisc"><a href=
"http://www.devx.com/upload/free/features/vcdj/2000/05may00/ens0500/ens0500-1.asp"
target=
"_top">http://www.devx.com/upload/free/features/vcdj/2000/05may00/ens0500/ens0500-1.asp</a></span></p>
</div>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
