Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simple answer is to use <code>vector</code>s instead of arrays. C++'s rules for passing arrays as function parameters are esoteric and derived from C. Here are some of the issues:</p> <ul> <li>You can't use arrays for long without understanding and using pointers</li> </ul> <p>Array subscripting is pointer subscripting. Arrays are accessed using pointer arithmetic. Arrays as function parameters are actually pointers in disguise.</p> <ul> <li>Functions don't get information about array size when taking an array argument</li> </ul> <p>Consider the declaration:</p> <pre><code>void inc_all(int myarray[]); /* increments each member of the array */ </code></pre> <p>Unfortunately, that array parameter is not an array parameter! It's actually a pointer parameter:</p> <pre><code>void inc_all(int *myarray); /* Exactly the same thing! */ </code></pre> <p>And a pointer doesn't know how many items are in the sequence it points at. As a result this function cannot have the information necessary to know when the array stops. You either need to pass the length:</p> <pre><code>void inc_all(int *myarray, size_t len); /* size_t rather than int */ </code></pre> <p>or you need to use a sentinel value to mark the end of the array. Either way, an array is not a self-contained encapsulated datatype like a <code>vector</code> is.</p> <ul> <li>You can't pass an arbitrarily-sized two-dimensional array to a function</li> </ul> <p>If you try to create a function which takes a two-dimensional array:</p> <pre><code>void inc_all(int myarray[][]); /* XXX won't compile! */ </code></pre> <p>it won't compile. The problem is you have an indeterminate length array of indeterminate length arrays of <code>int</code>s. The outer array doesn't know how large its members (the inner arrays) are and therefore doesn't know how to step through them in memory. You need to specify the size of the inner arrays:</p> <pre><code>void inc_all(int myarray[][10]); </code></pre> <p>at which point your code is probably not as general as you were hoping it was going to be.</p> <p>If you use <code>vector</code>s and <code>vector</code>s of <code>vectors</code>s, these problems don't arise because the <code>vector</code>s themselves know how many members they have and carry that information with them.</p> <p>If you still want to learn more about arrays and pointers I recommend <a href="http://c-faq.com/aryptr/index.html" rel="nofollow noreferrer">section 6 of the comp.lang.c FAQ</a>.</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.
    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