title: C program: Integer array pointer changes values when passed as parameter
body: <p>My problem is that my integer array's values change when it is passed to the function <strong>calculate</strong>. The values are correct for indexes 0, and 2->5.</p>
  
  <p>For some reason, indexes 1 and 6+ are not the correct values.</p>
  
  <p>Below is my code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  
  int* generate_rand (int length, int MAX_ARRAY);
  void calculate (int *array_ptr, int length, int *mean, int *sd);
  
  main() {
      srand(time(NULL));
      int a;
      printf("\nArray length?: ");
      scanf("%d", &amp;a);
      int* array_ptr2;
      array_ptr2 = generate_rand(a, 100);
      //int mean, sd;
      int* *mean;
      int* *sd;
      int i = 0;
      for (i = 0; i &lt; 10; i++) {
          printf("Array2: %d\n", *(array_ptr2 + i));
      }
      calculate(array_ptr2, a, *mean, *sd);
      //printf("Mean: %d\n", (int)*mean);
  }
  
  int* generate_rand (int length, int MAX_ARRAY) {
      int arr[length];
      int i;
      for (i = 0; i &lt; 10; i++) {
          int r = rand()%MAX_ARRAY;
          arr[i] = r;
          printf("Rand: %d\n", arr[i]);
      }
      int *arrPtr;
      arrPtr = &amp;arr[0];
      return arrPtr;
  }
  
  void calculate (int *array_ptr, int length, int *mean, int *sd) {
      int sum;
      int i;
      for (i = 0; i &lt; length; i++) {
          printf("Array: %d\n", *(array_ptr + i));
          sum += *(array_ptr + i);
          //array_ptr++;
          printf("Sum: %d, i:%d\n", sum, i);
      }
      //*mean = sum / length;
  }
  </code></pre>
  
  <p>Do you know what I'm doing wrong?</p>