Note that there are some explanatory texts on larger screens.

plurals
  1. POEquivalence of p[0] and *p for incomplete array types
    text
    copied!<p>Consider the following code (it came about as a result of <a href="https://stackoverflow.com/a/10183715/129570">this discussion</a>):</p> <pre><code>#include &lt;stdio.h&gt; void foo(int (*p)[]) { // Argument has incomplete array type printf("%d\n", (*p)[1]); printf("%d\n", p[0][1]); // Line 5 } int main(void) { int a[] = { 5, 6, 7 }; foo(&amp;a); // Line 10 } </code></pre> <p>GCC 4.3.4 <a href="http://ideone.com/rsrRA" rel="nofollow noreferrer">complains</a> with the error message:</p> <pre><code>prog.c: In function ‘foo’: prog.c:5: error: invalid use of array with unspecified bounds </code></pre> <p>Same error message in GCC 4.1.2, and seems to be invariant of <code>-std=c99</code>, <code>-Wall</code>, <code>-Wextra</code>.</p> <p>So it's unhappy with the expression <code>p[0]</code>, but it's happy with <code>*p</code>, even though these should (in theory) be equivalent. If I comment out line 5, the code compiles and does what I would "expect" (displays <code>6</code>).</p> <p>Presumably one of the following is true:</p> <ol> <li>My understanding of the C standard(s) is incorrect, and these expressions <em>aren't</em> equivalent.</li> <li>GCC has a bug.</li> </ol> <p>I'd place my money on (1).</p> <p><strong>Question:</strong> Can anyone elaborate on this behaviour?</p> <p><strong>Clarification:</strong> I'm aware that this can be "solved" by specifying an array size in the function definition. That's not what I'm interested in.</p> <hr> <p><strong>For "bonus" points:</strong> Can anyone confirm that MSVC 2010 is in error when it rejects line 10 with the following message?</p> <pre><code>1&gt;&lt;snip&gt;\prog.c(10): warning C4048: different array subscripts : 'int (*)[]' and 'int (*)[3]' </code></pre>
 

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