Array Function

Hi all,
I am making a helper function that returns that maximum value, minimum value and average. The values that I get in the first round is correct. However, the serial monitor just stops after it displays the second time and the values are wrong. Can anyone explain the reason why? I have also checked that the input array always takes in the correct value.

uint16_t* getarrayinfo (const uint16_t* input_array){
  //Serial.print ("Input[1]:"); Serial.println (input_array[1]);
  //Serial.print ("Input[2]:"); Serial.println (input_array[2]);
  //Serial.print ("Input[3]:"); Serial.println (input_array[3]);
  //Serial.print ("Input[4]:"); Serial.println (input_array[4]);
  //Serial.print ("Input[5]:"); Serial.println (input_array[5]);
  uint16_t* ret_array;
  uint16_t counter = 1;
  uint16_t max_value = input_array[1];
  uint16_t min_value = input_array[1];
  uint16_t avg_value = input_array[1];
  while (counter <= TestArraySize){
    if (max_value < input_array[counter]){
      max_value = input_array[counter];
    }
    if (min_value > input_array[counter]){
      min_value = input_array[counter];
    }
    avg_value = avg_value + input_array[counter];
    counter++;
  }
  avg_value = avg_value / TestArraySize;
  ret_array[1] = max_value;
  ret_array[2] = min_value;
  ret_array[3] = avg_value;
  Serial.print ("ret_array[1]:"); Serial.println (ret_array[1]);
  Serial.print ("ret_array[2]:"); Serial.println (ret_array[2]);
  Serial.print ("ret_array[3]:"); Serial.println (ret_array[3]);
  return ret_array;
}

void loop() {
  // put your main code here, to run repeatedly:
  uint16_t val[TestArraySize];
  uint16_t* ret_val;
  val[1] = (uint16_t) 2;
  val[2] = (uint16_t) 3;
  val[3] = (uint16_t) 6;
  val[4] = (uint16_t) 10;
  val[5] = (uint16_t) 1;
  ret_val = getarrayinfo(val);
}

I'm guessing TestArraySize == 5

WHY DON'T YOU POST YOUR CODE?

Yes TestArraySize is 5

Damn, I really am good.

There’s a lot wrong here.

Biggest problem, IMO, is that you’re creating a pointer (ret_array) and then using it like an array. That might be OK, but you’re neither allocating any storage for the array nor setting the pointer to point to said storage. So, you’re just writing data to arbitrary locations in memory. Not going to end well.

How do I return an array of values then? Im really sorry, I am new to this.

Well, start by remembering that arrays index from zero.

junpun95:
How do I return an array of values then?

You can't. The two options are to return a struct or to pass the array where you want the data as an argument to the function. That is, the array would belong to the caller.

I’d create the results array in the calling function, not the ‘getarrayinfo ()’ function. Pass it to ‘getarrayinfo()’ as an argument just like ‘input_array’. Then ‘getarrayinfo()’ can fill in the values.

An even easier way for newbies would be to make the results array global.

Hi, thank you all for your reply. The problem(I think) was that I was writing data to arbitrary locations and somehow, I'm using too much memory (which is why it stopped running after a few rounds). As for why its spitting out random values, I am still not sure. However, one more thing that I am really confused is that in the helper function returns the correct value, but now only the Average value in the loop function screws up. It always outputs 442. Anyone knows why?

uint16_t* getarrayinfo (const uint16_t* input_array){
  static uint16_t ret_array[3];
  uint16_t counter = 1;
  uint16_t max_value = input_array[1];
  uint16_t min_value = input_array[1];
  uint16_t avg_value = input_array[1];
  while (counter <= TestArraySize){
    if (max_value < input_array[counter]){
      max_value = input_array[counter];
    }
    if (min_value > input_array[counter]){
      min_value = input_array[counter];
    }
    avg_value = avg_value + input_array[counter];
    counter++;
  }
  avg_value = avg_value / TestArraySize;
  ret_array[1] = max_value;
  ret_array[2] = min_value;
  ret_array[3] = avg_value;
  //Serial.print ("ret_array[1]:"); Serial.println (ret_array[1]);
  //Serial.print ("ret_array[2]:"); Serial.println (ret_array[2]);
  //Serial.print ("ret_array[3]:"); Serial.println (ret_array[3]);
  return ret_array;
}

void loop() {
  // put your main code here, to run repeatedly:
  uint16_t val[TestArraySize];
  static uint16_t* ret_val;
  val[1] = (uint16_t) 2;
  val[2] = (uint16_t) 3;
  val[3] = (uint16_t) 6;
  val[4] = (uint16_t) 10;
  val[5] = (uint16_t) 1;
  ret_val = getarrayinfo(val);
  Serial.print ("Max:"); Serial.println (ret_val[1]);
  Serial.print ("Min:"); Serial.println (ret_val[2]);
  Serial.print ("Average:"); Serial.println(ret_val[3]);
}

A five element array has no element with the index 5.

PLEASE POST ALL YOUR CODE.

junpun95:
Anyone knows why?

Still because arrays start with index 0 and go up to index numberOfElements-1.

Thank you so much everyone. I changed the index and it works fine. Thank you again for your time!