1

I'm new to c++, and I want to pass a float array out of a function using a pointer. However, the returning array is always 0; I am running on an Arduino Uno.

This is my code. I want the readSensor() function to pass out a float array consisting of 3 floats. The float array should be pass to the pointer output*. But when I print out readings, it shows 0.00 instead of the values I passed out.

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

void loop() {
  float readings[3];
  readSensor(readings);
  for (int i = 0;i < 3;i++) {
    Serial.println(readings[i]);
  }
  delay(1000);
}

// pass out these 3 floats
float val1 = 3.14159;
float val2 = 2.741;
float val3 = 87;
void readSensor(float* output) {
  float container[3] = {val1, val2, val3};
  output = container;
}
4
  • 2
    What does ` output ` point to? An editable array of at least 3 elements? Commented May 26, 2019 at 3:19
  • 1
    Why do you use raw pointer in C++? Is this part of the interface? Commented May 26, 2019 at 3:20
  • what I hope to do is to pass the container array to the "output" arrray, which is the "readings" in my case. I'm really new maybe this is the wrong way to do it. Commented May 26, 2019 at 3:22
  • 1
    @Steven97102 You are using the wrong construct. Just use std::array, not dumb arrays, and then most of your code would remain the same. Commented May 26, 2019 at 3:46

3 Answers 3

5

You can't "pass an array out of a function" in the way you seek. C++ offers no such mechanism by simply using a pointer and a raw (C-style) array.

In your particular approach

void readSensor(float* output) {
 float container[3] = {val1, val2, val3};
 output = container;
}

container ceases to exist when the function returns. Also, output is passed by value, so the assignment output = container is not visible to the caller.

It is necessary to pass a pointer to (the first element of) an array into the function, and have the function copy data to it as needed. For example,

void readSensor(float* output)
{
     float container[3] = {1, 2, 3};
     for (int i = 0; i < 3; ++i)
        output[i] = container[i];
}

And the caller must supply the array and pass it.

int main()
{
     float result[3];
     readSensor(result);   // data will be copied into result
     // use result here
}

Bear in mind that the caller is responsible for calling the function correctly. For example if the caller of the function above passes an array with two elements, the behaviour is undefined.

A preferred way in C++ is to use a standard container, such as a std::vector. For example;

#include <vector>

std::vector<float> readSensor()
{
     float container[3] = {1, 2, 3};
     std::vector<float> output;
     for (int i = 0; i < 3; ++i)
        output.push_back(container[i]);
     return output;
}

int main()
{
     std::vector<float> result;
     result = readSensor();
     // use result here
}

If you don't know how to use standard containers, there is plenty of documentation around.

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

2 Comments

You can just return {1,2,3};.
@FrançoisAndrieux - yes, that is true. However, this question is obviously asked by a beginner. And that syntax will look like it is returning an array, even though it is doing something quite different (constructing a vector using an initialiser list and returning that).
2
void readSensor(float* output) 
{
    float container[3] = {val1, val2, val3};
    for (int i=0; i<3; i++)
    {
        output[i] = container[i];
    }   
}

5 Comments

Good answer, but it would be helpful to explain why.
Technically correct, but it's important to explain that this is a bad way of returning an array.
Can you explain what's a better approach?
@Steven97102 Use std::array and never look back.
@Steven97102 I think this link's description will help you
1
void readSensor(float* output) {
  float container[3] = {val1, val2, val3};
  output = container;
}

Here you are creating an array container on the stack and initializing it, and assigning the address of its first element to output. You then destroy container when you reach the end of the function, and by the time the function returns, the array that output used to point to no longer exists.

j.doe's answer is one solution to this, but you have to be careful to ensure output already points to an array of sufficient size before assigning values to it. You could also consider looking into std::array or std::vector.

2 Comments

Not only does the array that output used to point to no longer exist, but output also no longer exists. Assigning a value to a non-reference parameter does not change anything in the calling code.
@JaMiT You're right, I missed the way in which the OP was calling the function. Good catch

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.