Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The type of <code>a</code> is <code>int[2]</code>, not <code>int*</code>. Try again with</p> <pre><code>extern int a[2]; </code></pre> <hr> <p>The C compiler <strong>cannot type-check across source files</strong>. Therefore, when you say <code>int* a</code>, the compiler will assume you're telling the truth, use pointer semantics, and will not issue any compiler error.</p> <p>There are subtle difference between arrays and pointers. Let's assume a 32-bit system. Then the content of "a" will be distributed like this:</p> <pre> a 0x100 0x104 0x108 ← address +-----------+----------+ | 3 | 2 | ← content +-----------+----------+ </pre> <p>When <code>a</code> is an <strong>array</strong>,</p> <ul> <li>The value of the expression <code>a</code> is will be converted to the address of <code>a</code>. Therefore, when you print <code>a</code>, you will get its address, i.e. "0x100".</li> <li>The operation <code>a[n]</code> in C is equivalent to <code>*(a + n)</code>, i.e. advance the address <code>a</code> by <code>n</code> units, and then dereference it to get the content. Therefore, <code>a[0]</code> is equivalent to <code>*0x100</code>, which returns the content at 0x100, i.e. "3".</li> </ul> <p>When <code>a</code> is a <strong>pointer</strong>,</p> <ul> <li>The "value" of <code>a</code> is the content at the provided address. In fact this is the norm, the array type is a special case. Therefore, when you print <code>a</code>, you will get the content at that address, i.e. "3".</li> <li>The operation <code>a[n]</code> is still <code>*(a + n)</code>. Therefore, <code>a[0]</code> is equivalent to <code>*3</code>, which causes segmentation fault because the address "3" is invalid.</li> </ul>
    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