Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>*(record + i)</code> is not in fact an address. <code>record</code> is an address, and so is <code>(record + i)</code>, but <code>*(record + i)</code> is the <em>value stored at the address represented by</em> <code>(record + i)</code>. Therefore, calling <code>function(record[i])</code> is the same as <code>function(*(record + i))</code>, which will pass the <em>value</em> of the array element to the function, not a pointer.</p> <p>The syntax <code>&amp;record[i]</code> is not taking the address of an address. It is taking the address of <code>record[i]</code>, which is an object. The braces have a higher precedence than the ampersand, so <code>&amp;record[i]</code> is equivalent to <code>&amp;(record[i])</code>. You can think of it as expanding to <code>&amp;(*(record + i))</code> and then simplifying to <code>(record + i)</code>.</p> <p><strong>Update:</strong> To address your question from the comment, an array "decays" into a pointer if you reference the name of the array by itself. If you add square brackets <code>[]</code>, you will get a <em>value</em> from within the array. So, for your example, say you have an array of structures:</p> <pre><code>struct A { ... char abc[10]; ... } record[10]; </code></pre> <p>Then, you would have:</p> <ul> <li><code>record[i]</code> - an object of type <code>struct A</code> from the <code>record</code> array</li> <li><code>record[i].abc</code> - the <code>abc</code> array inside a particular <code>record</code> object, decayed to a pointer</li> <li><code>record[i].abc[k]</code> - a specific character from the string</li> <li><code>&amp;record[i].abc[0]</code> - one way of creating a pointer to the string</li> </ul> <p>The notation <code>record[i]-&gt;abc</code> that you mention in your comment cannot be used, since <code>record[i]</code> is an object and not a pointer.</p> <p><strong>Update 2:</strong> In regards to your second comment, the same rules described above apply regardless of how you nest the array within a structure (and whether you access that structure directly or through a pointer). Accessing an array using <code>arrayname[index]</code> notation will give you an item from the array. Accessing an array using <code>arrayname</code> notation (that is, using the array name by itself) will give you a pointer to the first element in the array. If you need more details regarding this phenomenon, here are a couple of links that explain arrays and the way that their names can decay into pointers:</p> <ul> <li><a href="http://boredzo.org/pointers/" rel="nofollow noreferrer">http://boredzo.org/pointers/</a></li> <li><a href="http://www.ibiblio.org/pub/languages/fortran/append-c.html" rel="nofollow noreferrer">http://www.ibiblio.org/pub/languages/fortran/append-c.html</a></li> <li><a href="http://c-faq.com/aryptr/index.html" rel="nofollow noreferrer">http://c-faq.com/aryptr/index.html</a></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. 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