Getting strange output from function when adding digitalRead()

Hi

I've a larger project where I'm using a function to return an array for later use. First I got strange returnvalues from the function, like 512 instead of 0 or 1. When I commented almost all code in the function it worked.

I've broken it down to this, and it seems to be when adding digitalRead the problem begins.

int* arr0[13];
const int buttonPin=3;
int buttonState=0;

void setup(){
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

// the loop routine runs over and over again forever:
void loop(){
  arr0[0]=myFunction(5,7,buttonPin);
  Serial.println(arr0[0][1]);
}

int* myFunction(int var0, int var1, int var2){
  buttonState=digitalRead(buttonPin);
  int arr1[3];
  arr1[0]=var0;
  arr1[1]=var1;
  arr1[2]=var2;
  return arr1;
}

I see the code as follow:
I define an array named arr0 with length 13

I call the function with myFunction(5, 7, buttonPin) and save returnvalue at index 0 in array arr0
I want to print out number 7 that I've passed to myFunction. It should be placed at arr0[0][1]?
buttonPin is 3 as stated earlier in code
I then define a array named arr1 with length 3
index 0 is filled with 5
index 1 is filled with 7
and index 2 is filled with buttonPin (3)
arr1 is returned

Why am I not getting the value that I'm looking for?

Best Regards
Niclas

I've a larger project where I'm using a function to return an array for later use.

Then, it is not written in C or C++ for the Arduino. Functions can not return arrays.

I define an array named arr0 with length 13

That is an array of pointers. They are all initialized to NULL.

  int arr1[3];

This is a local array, on the heap.

}

It goes away here, but

  return arr1;

Here, you've returned a pointer to that memory location.

  Serial.println(arr0[0][1]);

And here you try to dereference that pointer that now points to garbage.

You need to pass the function an array to populate. That array needs to be statically defined.

Code:

int arr1[3];

This is a local array, on the heap.

For "heap" read "stack".