Returning an array

I'm trying to figure out how to return an array from a function.

I can return the array itself with the below code but I can't figure out how to recieve an element from that array.

void setup() {
  // put your setup code here, to run once:
  Serial.begin (115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  int ret = Test();
  Serial.println(ret);
}


int Test(){
  int test[] = {1,2,3};
  return test;
}

Any tips would be appreciated.

You can return a struct containing an array, but I don't think you can return an array by itself.

What are you trying to do?

You can’t return a whole array. Usually what you do in this case is pass in a pointer to the array you want to fill.

You also can’t print the contents of an array in one line like that. Use a for loop.

void setup() {
  // put your setup code here, to run once:
  Serial.begin (115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  int ret[3] = {0,0,0};
  Test(ret);
  for(int i = 0; i<3; i++){
     Serial.print(ret[i]);
  }
  
}


void Test(int anArray[3]){
  
     //  Fill with 1, 2, 3
     for(int i = 0; i<3; i++){
         anArray[i] = i + 1;
     }
  
}

Use pointers.

Paul

Paul_KD7HB:
Use pointers.

Paul

...but not to an automatic variable within the function returning the pointer.

As stated, use pointers. But, then the local variable being returned must be ‘static’. Also, you need some way of knowing the array’s size in the calling function:

void setup() {
  Serial.begin (115200);
}

void loop() {
  int *returnedArray;
  returnedArray = Test();
  for (uint8_t i = 0; i < 3; i++) {
    Serial.println(returnedArray[i]);
  }
}

int *Test() {
  static int test[] = {1, 2, 3};
  return test;
}

As also previously stated, a more common technique would be to allocate the array storage in the calling program and pass a pointer to it (along with array size) to the function:

void setup() {
  Serial.begin (115200);
}

void loop() {
  const uint8_t arraySize = 3;
  int myArray[arraySize];
  test(myArray, arraySize);
  for (uint8_t i = 0; i < 3; i++) {
    Serial.println(myArray[i]);
  }
}

void test(int *theArray, size_t numElements) {
  for (uint8_t i = 0; i < numElements; i++) {
    theArray[i] = i;
  }
}

Thanks a million guys. I will give this a shot but I'm sure I won't have any problems now.

gfvalvo:
As also previously stated, a more common technique would be to allocate the array storage in the calling program and pass a pointer to it (along with array size) to the function:

void setup() {

Serial.begin (115200);
}

void loop() {
  const uint8_t arraySize = 3;
  int myArray[arraySize];
  test(myArray, arraySize);
  for (uint8_t i = 0; i < 3; i++) {
    Serial.println(myArray[i]);
  }
}

void test(int *theArray, size_t numElements) {
  for (uint8_t i = 0; i < numElements; i++) {
    theArray[i] = i;
  }
}

I do have one more question that will help me understand your code. What dose the “*” mean in front of *theArray? The code does work without it.

Thanks

KrafterHD:
I do have one more question that will help me understand your code. What dose the "*" mean in front of *theArray? The code does work without it.

Thanks

The * indicates a pointer.
Without it, the code DOES NOT work.
It won't even compile.

KrafterHD:
I do have one more question that will help me understand your code. What dose the "*" mean in front of *theArray? The code does work without it.

Thanks

https://www.arduino.cc/reference/en/language/structure/pointer-access-operators/dereference/

AWOL:
The * indicates a pointer.
Without it, the code DOES NOT work.
It won't even compile.

Now I know. Thank you very much. The code works great and can be easily modified for my needs.

Perehama:
* - Arduino Reference

Great. Thanks. I tried googling it but didnt come up with anything.