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.
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.
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?