Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What he means is that you cannot return the struct by-value in the header, because for that, the struct must be completely declared. But that happens in the C file (the declaration that makes <code>X</code> a complete type is "hidden" in the C file, and not exposed into the header), in his example. The following declares only an incomplete type, if that's the first declaration of the struct</p> <pre><code>struct X; </code></pre> <p>Then, you can declare the function</p> <pre><code>struct X f(void); </code></pre> <p>But you cannot define the function, because you cannot create a variable of that type, and much less so return it (its size is not known). </p> <pre><code>struct X f(void) { // &lt;- error here // ... } </code></pre> <p>The error happens because "x" is still incomplete. Now, if you only include the header with the incomplete declaration in it, then you cannot call that function, because the expression of the function call would yield an incomplete type, which is forbidden to happen. </p> <p>If you were to provide a declaration of the complete type <code>struct X</code> in between, it would be valid</p> <pre><code>struct X; struct X f(void); // ... struct X { int data; }; struct X f(void) { // valid now: struct X is a complete type // ... } </code></pre> <p>This would apply to the way using <code>typedef</code> too: They both name the same, (possibly incomplete) type. One time using an ordinary identifier <code>X</code>, and another time using a tag <code>struct X</code>. </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