Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to use consistent types for objects declared across different translation units. </p> <p>Given <code>int a[] = {2, 3};</code>, either of the declarations <code>extern int a[];</code> or <code>extern int a[2]</code>; would be compatible whereas <code>extern int *a;</code> would not as pointers and arrays are completely separate types.</p> <p>The one thing special about arrays is the when the name of an array appears in any expression context other than as an operand to "address of" (unary <code>&amp;</code>) or <code>sizeof</code>, they are automatically converted to a pointer to their first element. This is what provides the syntax compatibility between arrays and pointers, but they are not the same type.</p> <p>Consider these two functions for a commented example. Note that the expression <code>a</code> is converted to a pointer to its first element in the second (and technically third) <code>printf</code> of the first function, but not in the first <code>printf</code> where it is the operand to <code>&amp;</code>.</p> <pre><code>#include &lt;stdio.h&gt; void print_array_info(void) { extern int a[]; printf("address of a: %p\n", (void*) &amp;a); // prints address of a printf(" converted a: %p\n", (void*) a); // prints address of a[0] printf("value of a[0]: %x\n", a[0]); // prints value of a } void print_pointer_info(void) { extern int a[]; int *b = a; // == &amp;a[0] printf("address of b: %p\n", (void*) &amp;b); // prints address of b printf(" value of b: %p\n", (void*) b); // prints value of b (== &amp;a[0]) printf("value of b[0]: %x\n", b[0]); // prints value of b[0] (== a[0]) } </code></pre> <p>Note that I use <code>%p</code> to print pointers and explicitly cast to <code>void*</code>.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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