0

I am trying to use the values of an array in some other function, but when I return via a function the contents is wrong.

This is the external function file I'm trying to use

#include "Arrayreturner.h"

Arrayreturner::Arrayreturner() { }

void Arrayreturner::setResults(int* res, int size) {
 int temp[size];

 for(int i=0;i<size;i++) {
  temp[i] = res[i];
 }

 results = temp;

}

int* Arrayreturner::getResults() {
  return results;
}

This is the header file

#ifndef ARRAYRETURNER_H
#define ARRAYRETURNER_H

#include <Arduino.h>
#include <Wire.h>

class Arrayreturner {
public: // some public definitions first

 Arrayreturner();

void setResults(int* res, int size);
int* getResults();
int* results;

};


#endif

And here is the main program

#include <Arduino.h>
#include "Arrayreturner.h"

Arrayreturner data;

void setup() {
 Serial.begin(9600);

}

int* returnArray(int arr[]) {
 // int arr[] = { 3, 5, 6, 7, 9 };
 int *p = arr;

 // Serial.print("rA: ");
 // for(int i=0;i<5;i++) {
 //   Serial.print(p[i]); Serial.print(" ");
 // } Serial.println("");
 return p;
}

Arrayreturner testfunc() {
 Arrayreturner data;
 int arr[] = { 2,4,6,8,10};

 data.setResults(arr, 5);

 return data;
}

void loop() {

  int arr[] = { 3, 5, 6, 7, 9 };
  // int *p = arr;
  // int (*ptr)[5] = &arr;

 // Serial.print("p: ");
 // for(int i=0;i<5;i++) {
 //   Serial.print(p[i]); Serial.print(" ");
 // } Serial.println("");

 // int* p1;
 // p1 = returnArray(arr);
 // Serial.print("p1: ");
 // for(int i=0;i<5;i++) {
 //   Serial.print(p1[i]); Serial.print(" ");
 // } Serial.println("");

 // data.setResults(arr, 5);

 // int* res = data.getResults();
 //
 // Serial.print("res: ");
 // for(int i=0;i<5;i++) {
 //   Serial.print(res[i]); Serial.print(" ");
 // } Serial.println(" ");

 Arrayreturner ar = testfunc();
 int* res = ar.getResults();
 Serial.print("res: ");
 for(int i=0;i<5;i++) {
   Serial.print(res[i]); Serial.print(" ");
 } Serial.println(" ");

 Serial.println("--------------");



  delay(2000);

}

There is probably something wrong with the pointer declaration, but I cant figure it out.

Thanks for the help

0
0

This is a C language question. You copy a local array created on the stack of the testfunc() to other array created on the stack of Arrayreturner::setResults(). Then you store a pointer of the second array into member variable results. But that pointer is not valid when the function setResults() returns and the array on the stack is destroyed.

Normal way is to create an array, pass it as parameter to the function and let the function fill it.

1
  • I added my own variable (Arrayreturner) before calling the "testfunc" and passed it through the called functions. The array and the other variables of the Arrayreturner now show the correct values. Thanks – v3xX Sep 4 '18 at 16:25
0

Your arrays are declared locally to the functions. You cannot return local arrays from a function - as soon as you return the arrays no longer exist.

Not the answer you're looking for? Browse other questions tagged or ask your own question.