Error passing variable out of a function, beginner

I'have been using arduino for a month now, but have now moved on to writing libraries. However I have encountered this new error which I have not yet seen before when writing scripts.

"error: invalid conversion from 'char*' to 'int'"

This refers to a function inside my library source file,

int ADNS2610::motion()
{
   char arrayMotion[2];
   arrayMotion[0] = readOperation(DELTA_X);
   arrayMotion[1] = readOperation(DELTA_Y);
   return arrayMotion;

// readOperation is another function which returns a byte data type. 

}

It might be fundamental problem that I just cant see but I would really appreciate it if someone could please explain what should the return data type be (in this case i have it as int ADNS2610::motion()) and also the data type that is declared for a variable that is calling this function ( such as int x = motion()).
For this particular function i want the resulting output variable to be of char or int8_t form (-128 to 127).
I have also encountered the same error invblving another function which also uses an array. Perhaps it is how I use arrays?

Thanks for the help in advance.

You should not return an array that is declared in a function. What actually gets returned is a pointer to the array ( a pointer to a char in your example) but the data in the array will not be valid after the function returns.

You could declare the array static if you needed to pass the data back but getting a pointer to a character array when the user actually wants the x and y values is not user friendly. why not do this:

char ADNS2610::xMotion()
{
return readOperation(DELTA_X);
}

char ADNS2610::yMotion()
{
return readOperation(DELTA_Y);
}

A third possibility could be this:

void ADNS2610::motion(int &x, int &y){
  x = readOperation(DELTA_X);  
  y = readOperation(DELTA_Y);  
}

No you can use it like:

ADNS2610 adns = ADNS2610();
int x,y;
void setup(){
  Serial.begin(9600);//output

  //usage example
  adns.motion(x,y); //get x/y

  Serial.print(x);
  Serial.print(" , ");
  Serial.println(y);
}

:slight_smile:

Thanks alot for the replies, finally back on track.

:slight_smile: