Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In C, when you used the name of an array in an expression (including passing it to a function), unless it is the operand of the address-of (<code>&amp;</code>) operator or the <code>sizeof</code> operator, it <em>decays</em> to a pointer to its first element.</p> <p>That is, in most contexts <code>array</code> is equivalent to <code>&amp;array[0]</code> in both type and value.</p> <p>In your example, <code>my_array</code> has type <code>char[100]</code> which decays to a <code>char*</code> when you pass it to printf.</p> <p><code>&amp;my_array</code> has type <code>char (*)[100]</code> (pointer to array of 100 <code>char</code>). As it is the operand to <code>&amp;</code>, this is one of the cases that <code>my_array</code> doesn't immediately decay to a pointer to its first element.</p> <p>The pointer to the array has the same address value as a pointer to the first element of the array as an array object is just a contiguous sequence of its elements, but a pointer to an array has a different type to a pointer to an element of that array. This is important when you do pointer arithmetic on the two types of pointer.</p> <p><code>pointer_to_array</code> has type <code>char *</code> - initialized to point at the first element of the array as that is what <code>my_array</code> decays to in the initializer expression - and <code>&amp;pointer_to_array</code> has type <code>char **</code> (pointer to a pointer to a <code>char</code>).</p> <p>Of these: <code>my_array</code> (after decay to <code>char*</code>), <code>&amp;my_array</code> and <code>pointer_to_array</code> all point directly at either the array or the first element of the array and so have the same address value.</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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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