Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You cannot have memory with no value in it. It's physically impossible. It's due to the laws of physics of our universe :-)</p> <p>Also, this:</p> <pre><code>Acct clear[0]; </code></pre> <p>is wrong. You cannot have an array with zero elements. Some compilers will allow this as an extension, but it's not valid C. And for the compilers that allow this, it doesn't do what you think it does.</p> <p>It would seem to me that what you want instead is to resize the array. To do that, you would need to copy the elements you want to keep into a new array, and then free() the old one. To do that, you need to create <code>dism2A03</code> using dynamic memory:</p> <pre><code>Acct *dism2A03 = malloc(sizeof(Acct) * MAX_ENTRY); if (dism2A03 == NULL) { // Error: We're out of memory. } </code></pre> <p>(malloc() returns <code>NULL</code> if there's no more free memory, and the code checks that. Usually all you can do if this happens is terminate the program.)</p> <p>When you want a new array with some elements removed, then you should back up the starting address of the current one:</p> <pre><code>Acct* oldArray = dism2A03; </code></pre> <p>then create a new one with the new size you want:</p> <pre><code>dism2A03 = malloc(sizeof(Acct) * NEW_SIZE); if (dism2A03 == NULL) { // Error: We're out of memory. } </code></pre> <p>copy the elements you want from the old array (<code>oldArray</code>) to the new one (<code>dism2A03</code>) - which is up to you, I don't know which ones you want to keep - and after than you must free the old array:</p> <pre><code>free(oldArray); </code></pre> <p>As a final note, you might actually not want to create a new array at all. Instead, you could keep having your original, statically allocated array ("statically allocated" means you're not using malloc()):</p> <pre><code>Acct dism2A03[MAX_ENTRY]; </code></pre> <p>and have a index variable where you keep track of how many useful elements are actually in that array. At first, there are 0:</p> <pre><code>size_t dism2A03_size = 0; </code></pre> <p>As you add elements to that array, you do that at the position given by <code>dism2A03_size</code>:</p> <pre><code>dism2A03[dism2A03_size] = &lt;something&gt; ++dism2A03_size; // Now there's one more in there, so remember that. </code></pre> <p>While doing so, you need to make sure that <code>dism2A03_size</code> does not grow larger than the maximum capacity of the array, which is <code>MAX_ENTRY</code> in your case. So the above would become:</p> <pre><code>if (dism2A03_size &lt; MAX_SIZE) { dism2A03[dism2A03_size] = &lt;something&gt; ++dism2A03_size; // Now there's one more in there, so remember that. } else { // Error: the array is full. } </code></pre> <p>As you can see, adding something to the end of the array is rather easy. Removing something from the end of the array is just as easy; you just decrement <code>dism2A03_size</code> by one. However, "removing" something from the middle of the array means copying all following elements by one position to the left:</p> <pre><code>for (size_t i = elem_to_remove + 1; i &lt; dism2A03_size; ++i) { dism2A03[i - 1] = dism2A03[i]; } --dism2A03_size; // Remember the new size, since we removed one. </code></pre> <p>Note that you should not attempt to remove an element if the array is empty (meaning when <code>dism2A03_size == 0</code>.)</p> <p>There's also the case of adding a new elements in the middle of the array rather than at the end. But I hope that now you can figure that out on your own, since it basically a reversed version of the element removal case.</p> <p>Also note that instead of copying elements manually one by one in a <code>for</code> loop, you can use the memcpy() function instead, which will do the copying faster. But I went with the loop here so that the logic of it all is more obvious (hopefully.)</p>
 

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