Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take it easy. It is only a compiler error. Arrays are pretty tricky. Here is the <em>rule</em>:</p> <blockquote> <p>The value of a variable of type array <strong>decays</strong> to the <strong>address of element zero</strong> of this array</p> </blockquote> <p>Your first snippet looks like:</p> <pre><code>int a[2] = {1,2}; </code></pre> <p>So according to the rule if <code>a</code> is in the right hand side of a assignment then it decays to address of the element zero and that is why it has type <code>int *</code>. This brings you to</p> <pre><code>int *b = a; </code></pre> <p>In the second snippet what you really have is an array of arrays. (By the way, to make it explicit I've changed your code a bit.)</p> <pre><code>int a[2][2]={{1,2},{3,4}}; </code></pre> <p>This time <code>a</code> will decay to the pointer to an <em>array of two integers</em>! So if you would want to assign <code>a</code> to something, you would need this something to have the same type.</p> <pre><code>int (*b)[2] = a; //Huh! </code></pre> <p>(This syntax maybe a bit stunning to you, but just think for a moment that we have written <code>int *b[2];</code> Got the point? <code>b</code> would be an array of pointers to integers! Not really what we wanted...)</p> <p>You could stop reading here, but you could also move on, because I have not told you all the truth. The <em>rule</em> I mentioned has three exceptions...</p> <p>The value of the array will <strong>not</strong> decay to the address of the element zero <strong>if</strong></p> <ol> <li>array is operand of <code>sizeof</code></li> <li>array is operand of <code>&amp;</code></li> <li>array is a literal string initializer for a character array</li> </ol> <p>Let's explain these exceptions in more detail and with examples:</p> <pre><code>int a[2]; int *pi = a ; /* the same as pi = &amp;a[0]; */ printf("%d\n", sizeof(a)); /* size of the array, not of a pointer is printed! */ int (*pi2)[2] = &amp;a; /* address of the array itself is taken (not the address of a pointer) */ </code></pre> <p>And finally</p> <pre><code>char a[] = "Hello world "; </code></pre> <p>Here not a pointer to "Hello world" is copied, but the whole string is copied and a points to this copy.</p> <p>There is <em>really</em> a lot of information and it is really difficult to understand everything at once, so take your time. I recommend you to read K&amp;R on this topic and afterwards <a href="http://rads.stackoverflow.com/amzn/click/0131774298">this</a> excellent book.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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