Note that there are some explanatory texts on larger screens.

plurals
  1. POWhats the difference? Pointer to an array vs regular array
    primarykey
    data
    text
    <p>I'm familiar with Java and trying to teach myself C/C++. I'm stealing some curriculum from a class that is hosting their materials <a href="http://courses.washington.edu/css342/zander/css332/array.html" rel="nofollow">here</a>. I unfortunately can't ask the teacher since I'm not in the class. My concern is with the section under "dynamically declared arrays":</p> <blockquote> <p>If you want to be able to alter the size of your array at run time, then declare dynamic arrays. These are done with pointers and the new operator. For the basics on pointers, read the pointers section.</p> <p>Allocate memory using new, and then you access the array in the same way you would a static array. For example,</p> <p>int* arrayPtr = new int[10]; for (int i = 0; i &lt; 10; i++) { arrayPtr[i] = i; }</p> <p>The memory picture is identical to the static array, but you can change the size if you need to. Don't forget you must deallocate the memory before allocating new memory (or you will have a memory leak).</p> <p>delete [] arrayPtr; // the [] is needed when deleting array pointers arrayPtr = new int[50]; . . .</p> <p>When you're completely done with the array, you must delete its memory:</p> <p>delete [] arrayPtr;</p> <p>Dynamic multi-dimensional arrays are done in a similar manner to Java. You will have pointers to pointers. For an example, see a</p> </blockquote> <p>My understanding is that an array in C is simply a reference to the memory address of the first element in the array. </p> <p>So, what is the difference between <code>int *pointerArray = new int[10];</code> and <code>int array[10];</code> if any?</p> <p>I've done some tests that seem to indicate that they do the exact same thing. Is the website wrong or did I read that wrong?</p> <pre><code>#include &lt;cstdlib&gt; #include &lt;iostream&gt; using namespace std; int main(int argc, char** argv) { // Initialize the pointer array int *pointerArray = new int[10]; for (int i = 0; i &lt; 10; i++){ pointerArray[i] = i; } // Initialize the regular array int array[10]; for (int i = 0; i &lt; 10; i++){ array[i]= i; } cout &lt;&lt; *(pointerArray + 5) &lt;&lt; endl; cout &lt;&lt; *(array + 5) &lt;&lt; endl; cout &lt;&lt; pointerArray[5] &lt;&lt; endl; cout &lt;&lt; array[5] &lt;&lt; endl; cout &lt;&lt; pointerArray &lt;&lt; endl; cout &lt;&lt; array &lt;&lt; endl; return 0; } </code></pre> <p>Output:</p> <pre><code>5 5 5 5 0x8f94030 0xbfa6a37c </code></pre> <p>I've tried to "dynamically re-size" my pointer array as described on the site, but my new (bigger) pointer array ends up filled with 0's which is not very useful.</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.
 

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