Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's examine some simple code in a system where integers are four bytes long:</p> <pre><code>int xyzzy[] = {3,1,4,1,5,9,2,6,5,3,5,8,9}; // 13 integers printf ("%d\n"", sizeof(xyzzy)); // 13 * 4 = 52 printf ("%d\n"", sizeof(int)); // 4 printf ("%d\n"", sizeof(xyzzy) / sizeof(int)); // 52 / 4 = 13 </code></pre> <p>As per that final line, that calculation is a way to get the <em>number of items</em> in an array.</p> <p>As an aside, I prefer the construct:</p> <pre><code>sizeof(xyzzy) / sizeof(*xyzzy) </code></pre> <p>since that will continue to work even if I change the type of <code>xyzzy</code> to <code>double</code> for example. It means I only need to change the <em>one</em> line where the variable is declared rather than hunting for all the size calculations as well.</p> <p>In fact I even have a favorite macro for this:</p> <pre><code>#define numof(x) (sizeof(x) / sizeof(*x)) </code></pre> <p>to make my code a little smaller.</p> <p>In terms of what that <code>for</code> loop is doing exactly (and, by the way, it's not technically the second <code>for</code> loop since there's only one, but it <em>is</em> the second loop), it basically iterates through every value of <code>i</code> (staring at 0, the first index) until it reaches either the point beyond the last element or it finds an element with the desired item code.</p> <p>On exit from that loop, <code>i</code> will be set to the number of elements if the item code wasn't found or set to the correct index if it <em>was</em> found, hence the <code>if</code> statement following that <code>for</code> loop.</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.
    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