Receive array from function

I have created a class (.cpp file), in the class I have an array of bytes, like this:

byte myArray[];

Then I have a method that returns the array, like this:

byte getArray() {
    return myArray;

In the .ino file I'm importing the .cpp and creating an object based on the class to retrieve the array:

MyClass myObject = MyClass ();

byte myArray[len];
myArray= myObject.getArray();

When trying to compile the program I get this error:

invalid conversion from 'byte* {aka unsigned char*}' to 'byte {aka unsigned char}' [-fpermissive]

I'm not familiar with pointers so I'm not sure what the error means, any ideas? Thanks!

A pointer is "the address of" what ever is named. The only way to get familiar with pointer is to work with them. That means tracking down the cause of your compiler error.



You need to return a pointer to the array:

uint8_t * getArray()
    return myArray;

Use like this:

uint8_t * array = getArray();
Serial.println( array[0] );
Serial.println( array[1] );

You are trying to return an array as a single byte. You need to return an array instead. However if you do not specify a size, or a list to initialize it, then your array has no size (and isn't useful).

You can achieve this using references, you may need to do some reading to understand this though. This method does not copy the array, it uses a reference to it. If you really want to copy it, you will need a loop to copy each element.

I have used a typedef to make things a bit easier to read.

In a header, which both the sketch and cpp can see

typedef byte ArrayType[10];

ArrayType &getArray();

In the cpp:

ArrayType array = {1,2,3,4};

ArrayType &getArray(){
  return array;

And in the sketch:

void func() {
  ArrayType &arrayRef = getArray();

You can wrap the array and function in a class if you like.