0

I'm trying to create function that returns an array but when using

int x[5];

void setup() {
  x = getX();
}

int * getX(){
  return {1,1,1,1,1};
}

I get: incompatible types in assignment of 'int*' to 'int [5]'

1
  • 1
    To make assignment work on arrays you have to use std::array. In C there is no such feature like assignment between arrays so you have to use C++ specific feature. Commented Dec 17, 2019 at 13:20

1 Answer 1

1

Your Arduino-IDE may not support C++'s std::array like mentioned in a comment, but you can do it like:

int* get() {
    static int my_array[] = {1, 1, 1, 1, 1};
    return my_array;
}

It's no possible to reasign an array like you've tried in your setup function.

Consider using:

int* x = nullptr;

void setup() {
    x = get();
}

I've to point out, that this is not best practice. Maybe you can give some more information to find a better solution.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.