Function in a Class that returns an array.

Hi,

I'm trying to construct a function that returns two bytes simoultaniously.

byte() MyClass::MyFunction()
{
    byte MyBytes(2) = {a,b};
    return MyBytes;
}

Somewhere else...

byte something(2) = MyClass.MyFunction();

Thanks in advance!

Marco

void MyClass::MyFunction( byte MyBytes[], size_t MyBytesSize )
{
  if ( MyBytesSize >= 2 )
  {
    MyBytes[0] = 13;
    MyBytes[1] = 42;
  }
}
byte something[2];

MyInstance.MyFunction( something, sizeof(something)/sizeof(something[0]) );

You can't. And there are four reasons why that won't work (that I can think of).

Firstly, myBytes is a local variable so goes out of scope (memory is deleted) when you return.
Secondly, that is not how you declare an array.
Thirdly, C/C++ don't allow it - an array is really just a pointer to the start of a block of memory. If you want to return the array, you can only return a pointer to the memory it occupies (see first point).
Fourthly, you can't copy one array to another with an '=' sign.

You can do it another way though:

void MyClass::MyFunction(byte* MyBytes){
  MyBytes[0] = 'a';
  MyBytes[1] = 'b';
}

Called with:

byte something[2];
MyClass.MyFunction(something);
void MyClass::MyFunction( byte MyBytes[], size_t MyBytesSize )

{
  if ( MyBytesSize >= 2 )
  {
    MyBytes[0] = 13;
    MyBytes[1] = 42;
  }
}

I thought that created a local array. Should it not be this (argument is a pointer):

void MyClass::MyFunction( byte* MyBytes, size_t MyBytesSize )
{
  if ( MyBytesSize >= 2 )
  {
    MyBytes[0] = 13;
    MyBytes[1] = 42;
  }
}

I thought that created a local array.

No.

Should it not be this (argument is a pointer):

That also works. I prefer the prior syntax because it makes the expectation clear to the caller and the maintainer.

That makes much more sense. I'm not sure where I read that it made a local copy, but clearly it was wrong.

PHP documentation?

Fourthly, you can't copy one array to another with an '=' sign.

You can when the array is (properly) allocated AND assigned in one statement, as OP's code tried to do.

OP's code is wrong, though, for all the other reasons you state.

Thanks for all the responses, I'm new to accessing pointers through classes so bare with me.

I'm doing an I2C project and I want to send two byte data packets over the twi. I don't exactly have a lot of time to do a calculation, typecast and send the response once it has been requested. So I want to place a public pointer to the two bytes in my class. I will do the calculation and type cast from the main loop over an interval and update the public data in the class so that the TWI can simply read it when it needs to.

In my Class:

Class MyClass
{
   public:
      *byte TwoByteData;
   private:
      void StoreValue();
}

void StoreValue()
{
   word MyData = 12345;
   TwoByteData[0] = highByte(MyData);
   TwoByteData[1] = lowByte(MyData);
}

In my main program somewhere

Wire.write(MyClass.TwoByteData, 2)

Thanks in advance!

I don't exactly have a lot of time to do a calculation, typecast and send the response once it has been requested.

The cast takes no time at all.

Posting code that actually compiles is generally preferred over code that obviously won't.

Pre-splitting the int into two bytes doesn't save much time. The highByte() and lowByte() "functions" are macros that are resolved at compile time. The resulting code is simply some bit shifting and masking, which are very fast operations.

I think you are creating a mountain out of a molehill.

bigbro:
Hi,

I'm trying to construct a function that returns two bytes simoultaniously.

byte() MyClass::MyFunction()

{
    byte MyBytes(2) = {a,b};
    return MyBytes;
}




Somewhere else...


byte something(2) = MyClass.MyFunction();

You can return a struct with two bytes in it.

struct foo {
byte a,b;
};

foo myFunction() {
foo result;
result.a = 1;
result.b = 2;
return result;
};

foo t = myFunction();