Question with functions and arrays

Hello,

i want to implement a function that returns an array of booleans, like this one:

boolean[] MoppySerial::getBits(byte input){
    boolean output[8];
    for( int i = 0; i < 8; i++){
        output[i] = bitRead(input, 7-i);
    }
    return output;
}

but I get the error:

/var/folders/3y/x7dd3yxd3jgg17h3fnxz3gb80000gn/T/arduino_build_690945/sketch/src/MoppyNetworks/MoppySerial.cpp: In member function 'void MoppySerial::readMessages()':
/var/folders/3y/x7dd3yxd3jgg17h3fnxz3gb80000gn/T/arduino_build_690945/sketch/src/MoppyNetworks/MoppySerial.cpp:57:45: error: 'getBits' was not declared in this scope
     boolean firstBits[8] = getBits(firstByte);
                                             ^
/var/folders/3y/x7dd3yxd3jgg17h3fnxz3gb80000gn/T/arduino_build_690945/sketch/src/MoppyNetworks/MoppySerial.cpp: At global scope:
/var/folders/3y/x7dd3yxd3jgg17h3fnxz3gb80000gn/T/arduino_build_690945/sketch/src/MoppyNetworks/MoppySerial.cpp:75:8: error: expected unqualified-id before '[' token
 boolean[] MoppySerial::getBits(byte input){

I think the second part of the error is the more interesting one.
p.s. you can forget the MoppySerial::

Why do you want to waste eight times as much memory as you need?

You wouldn't ever want to return any kind of reference to an automatic variable either.

I might be able to forget MoppySerial but the compiler can't.

AWOL:
Why do you want to waste eight times as much memory as you need?

You wouldn't ever want to return any kind of reference to an automatic variable either.

Because I can, don't ask, it's only for testing

MorganS:
I might be able to forget MoppySerial but the compiler can't.

okay, but would such a function work, if their is no MoppySerial:: ?

i want to implement a function that returns an array of booleans, like this one:

You can’t return an array in C/C++.

Two alternatives:

  • Use an “out” parameter:
void MoppySerial::getBits(byte input, boolean* output){
    for( int i = 0; i < 8; i++){
        output[i] = bitRead(input, 7-i);
    }
}

Example:

byte input = 0x55;
boolean output[8];
MoppySerial::getBits(input,output);
for( int i = 0; i < 8; i++){
    Serial.print(output[i]);
}
  • Use a struct:
struct Output {
    boolean data[8];
}

Output MoppySerial::getBits(byte input){
    Output output;
    for( int i = 0; i < 8; i++){
        output.data[i] = bitRead(input, 7-i);
    }
    return output;
}

Example:

byte input = 0x55;
Output output = MoppySerial::getBits(input);
for( int i = 0; i < 8; i++){
    Serial.print(output.data[i]);
}

Because I can, don't ask, it's only for testing

But you shouldn't, and, as it turns out, you can't.

(oddly though, you could return a struct, containing an array. But you shouldn't do that either.)

You would have to make the local (automatic) array 'static' so that it continues to exist after the function exits. Then return a pointer to it.

A better way is to pass a pointer to an array (and possibly the array's size) into the function from the calling function. Then operate on that.