Note that there are some explanatory texts on larger screens.

plurals
  1. POSpiral rule and 'declaration follows usage' for parsing C and C++ declarations
    primarykey
    data
    text
    <p>This question follows <a href="https://stackoverflow.com/questions/3706704/whats-the-meaning-of-this-piece-of-code-void-signalint-sig-void-funcint">this other question about C declarations</a>. Reading the answer to this question, I read about the <a href="http://c-faq.com/decl/spiral.anderson.html" rel="nofollow noreferrer">spiral rule</a> and I also understood what "declaration follows usage" means.</p> <p>Ok so far. But then I read <a href="http://c-faq.com/decl/cdecl1.html" rel="nofollow noreferrer">this declaration</a>:</p> <pre><code>char *(*(*a[N])())(); </code></pre> <p>and I was wondering how to parse it with the "declaration follows usage" 'rule'. Especially for the array part.</p> <p>What I read is:</p> <pre><code>(*(*a[N])()) </code></pre> <p>is a function <code>()</code> returning a <code>char *</code>, then, dereferencing the following</p> <pre><code>(*a[N])() // 1 </code></pre> <p>is this 'function returning a <code>char*</code>', and so 1 is a 'pointer to a function returning <code>char *</code>' then I would say 'when <code>(*a[N])</code> is called, it is [previous declaration]'. At this point we have <code>(*a[N])</code> is a function returning a pointer to a function returning <code>char *</code>.</p> <p>But then I don't know how to apply this rule to distinguish between the 'pointer to array' and 'array of pointer'. </p> <p>Can someone clarify this?</p> <p>Other question: what are the rules of "precedence" in such declarations, between <code>&amp;</code> (in C++), <code>*</code>, and <code>[]</code>? [maybe "precedence" is not the correct term]</p> <hr> <p>To check if I understood the 'spiral rule' correctly, I also parse this expression below; please tell me if I am wrong.</p> <pre><code> +-----------+ | +------+ | | | +-+ | | | | ^ | | | char* (*(* a[N])())(); ^ ^ ^ ^ ^ ^ | | | | | | | | +---+ | | | +--------+ | +--------------+ </code></pre> <p>For me it is easier (loop by loop): </p> <ul> <li><code>a</code> is an array of N ...</li> <li>pointer to function returning ...</li> <li>pointer to function returning ...</li> <li><code>char *</code></li> </ul> <p>But I am maybe missing something which in that case let me obtain the correct answer but that could be wrong in another more complicated case.</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.
 

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