Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For understanding the difference, we need to understand two different <em>contexts</em>.</p> <ul> <li>In <em>value</em> contexts, the name of an array of type <code>T</code> is equivalent to a pointer to type <code>T</code>, and is equal to a pointer to the array's first element.</li> <li>In <em>object</em> contexts, the name of an array of type <code>T</code> does not reduce to a pointer.</li> </ul> <p>What is object context?</p> <p>In <code>a = b;</code>, <code>a</code> is in object context. When you taken the address of a variable, it's used in object context. Finally, when you use <code>sizeof</code> operator on a variable, it's used in object context. In all other cases, a variable is used in value context.</p> <p>Now that we have this knowledge, when we do:</p> <pre><code>void f(int arr[4]); </code></pre> <p>It is <em>exactly</em> equivalent to</p> <pre><code>void f(int *arr); </code></pre> <p>As you found out, we can omit the size (4 above) from the function declaration. This means that you can't know the size of the "array" passed to <code>f()</code>. Later, when you do:</p> <pre><code>int a[]={1,2,3,4}; f(a); </code></pre> <p>In the function call, the name <code>a</code> is in value context, so it reduces to a pointer to <code>int</code>. This is good, because <code>f</code> expects a pointer to an <code>int</code>, so the function definition and use match. What is passed to <code>f()</code> is the pointer to the first element of <code>a</code> (<code>&amp;a[0]</code>).</p> <p>In the case of</p> <pre><code>int a[]={1,2,3,4}; int b[4] = a; </code></pre> <p>The name <code>b</code> is used in a object context, and does not reduce to a pointer. (Incidentally, <code>a</code> here <em>is</em> in a value context, and reduces to a pointer.)</p> <p>Now, <code>int b[4];</code> assigns storage worth of 4 <code>int</code>s and gives the name <code>b</code> to it. <code>a</code> was also assigned similar storage. So, in effect, the above assignment means, "I want to make the storage location the same as the previous location". This doesn't make sense.</p> <p>If you want to <em>copy</em> the contents of <code>a</code> into <code>b</code>, then you could do:</p> <pre><code>#include &lt;string.h&gt; int b[4]; memcpy(b, a, sizeof b); </code></pre> <p>Or, if you wanted a pointer <code>b</code> that pointed to <code>a</code>:</p> <pre><code>int *b = a; </code></pre> <p>Here, <code>a</code> is in value context, and reduces to a pointer to <code>int</code>, so we can assign <code>a</code> to an <code>int *</code>.</p> <p>Finally, <em>when initializing an array</em>, you can assign to it explicit values:</p> <pre><code>int a[] = {1, 2, 3, 4}; </code></pre> <p>Here, a has 4 elements, initialized to 1, 2, 3, and 4. You could also do:</p> <pre><code>int a[4] = {1, 2, 3, 4}; </code></pre> <p>If there are fewer elements in the list than the number of elements in the array, then the rest of the values are taken to be 0:</p> <pre><code>int a[4] = {1, 2}; </code></pre> <p>sets <code>a[2]</code> and <code>a[3]</code> to 0.</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