title: C - using dynamic arrays
body: <p>I have a problem in C. This is the question: </p>
  
  <p>Develop a C function ADDER that adds two integer arrays together. ADDER should have only two parameters, which are the two arrays to be added. The second array argument will hold the sum of arrays on exit. Both parameters should be passed by reference. </p>
  
  <p>Write a C program to test the ADDER function with the call ADDER (A, A) where A is an array to be added to itself. Array A can be of any size with any values. Write, compile, and execute the program.</p>
  
  <p>Explain the results of the program.</p>
  
  <hr>
  
  <p>So far I have solved it this way and it works just fine:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  // using namespace std;
  
  const int SIZE = 5;
  
  /* Adds two arrays and saves the result in b
   * Assumes that b is larger than or equal to a in size
   */
  
  void ADDER(int (&amp;a)[SIZE], int (&amp;b)[SIZE]) {
      int aSize, bSize, i; /* variable declaration */
      /* find out the sizes first */
      aSize = sizeof (a) / sizeof (int);
      bSize = sizeof (b) / sizeof (int);
      /* add the values into b now */
      for (i = 0; i &lt; aSize; i++) {
      b[i] = b[i] + a[i];
      }
      /* we have the sum at the end in b[] */
  }
  
  /* Test program for ADDER */
  
  int main() {
  int i; /* variable declaration */
  int a[] = {1, 2, 3, 4, 5}; /* the first array */
  
  /* add them now */
  ADDER(a, a);
  /* print results */
  printf("\nThe sum of the two arrays is: ");
  for (i = 0; i &lt; SIZE; i++) {
      printf("%d ", a[i]); /* print each element */
  }
  return 0;
  }
  </code></pre>
  
  <p>The problem is, I have to use dynamic arrays and use malloc and realloc in the program to compute the size of the array on the fly. Instead of specifying the array size and the elements itself, I want the program to ask the user for input and the user enters the array and the size is determined there. It should all be dynamic. I do not know how this is done. Can anyone please help me out! thanks! </p>
  
  <p>Also I have to explain how the array is added to itself the result is saved in "a" and the original array is lost replaced by the sum. how can I explain this?</p>