Accessing struct return from a function

i Have declared the following function:

struct ab {
  byte LowLowByte;
  byte LowHighByte;
  byte HighLowByte;
  byte HighHighByte;
};

and accessing that in a function:

struct ab ConvertFloatToMultipleBytes(float Floatinputs){  

  struct ab ab_instance;
  uint32_t scaled = (Floatinputs * 1000);  

  ab_instance.LowLowByte = byte(scaled);
  ab_instance.LowHighByte = byte(scaled >> 8);
  ab_instance.HighLowByte = byte(scaled >> 16);
  ab_instance.HighHighByte = byte(scaled >> 24);

  return ab_instance;
}

Now i am not sure how to access this function in my loop function? now that i value is being returned, how do i access it back in loop ?

void loop() {
  struct ab asBytes;
  asBytes = ConvertFloatToMultipleBytes(1.234);
  Serial.print("First byte is: ");
  Serial.println(asBytes.LowLowByte, BIN);
  delay(10000);
}

But you know there's an easier way to do this using a union don't you?

But you know there's an easier way to do this using a union don't you?

Someone did recommend me using union. and i tried it. But i couldn't make it work. :( so i took this path.

First, a style note. You don’t need to say ‘struct ab’ everywhere you define a variable or function of type ‘ab’. Just ‘ab is enough:

ab ConvertFloatToMultipleBytes(float Floatinputs) {
}

You could do this in ‘loop()’:

void loop() {
  ab myLocalStruct = ConvertFloatToMultipleBytes(someFloatValue);
  // Now access members as:
  // myLocalStruct.LowLowByte
  // myLocalStruct.LowHighByte 
  // etc.
}

But, there’s a more efficient way. Pass a pointer to your local struct from loop() to the function():

struct ab {
  byte LowLowByte;
  byte LowHighByte;
  byte HighLowByte;
  byte HighHighByte;
};

void setup() {}

void loop() {
  ab myLocalStruct;

  ConvertFloatToMultipleBytes(someFloatValue, &myLocalStruct);
}

void ConvertFloatToMultipleBytes(float Floatinputs, ab *ptr) {
  uint32_t scaled = (Floatinputs * 1000);
  ptr->LowLowByte = byte(scaled);
  ptr->LowHighByte = byte(scaled >> 8);
  ptr->HighLowByte = byte(scaled >> 16);
  ptr->HighHighByte = byte(scaled >> 24);
}

gfvalvo: But, there’s a more efficient way. Pass a pointer to your local struct from loop() to the function():

...or even easier (cuz this is C++ dammit) you can just pass a reference and not have to worry about all that pointer dereferencing!

Yea, but I’ve always liked the ‘->’ notation ;D
Bet the generated assembler code is very similar if not identical.

The FAR bigger problem that nobody has yet pointed out is that the OPs code is returning a pointer to a stack-based structure which has gone out of scope which is very likely to be corrupted by a timer or serial interrupt handler before the calling code even reads it....

Regards, Ray L.

RayLivingston: The FAR bigger problem that nobody has yet pointed out is that the OPs code is returning a pointer to a stack-based structure....

Don’t think so. The function is defined as returning a variable of the type ‘ab’, not a pointer to one. So, just like returning any other type, a copy of the local variable is made and that’s what gets returned.

ammarqs: Someone did recommend me using union. and i tried it. But i couldn't make it work. :( so i took this path.

So rather than show it to you guys and get help making it work, I decided to do this harder thing and reinvent the wheel. Now help me with that.

RayLivingston: The FAR bigger problem that nobody has yet pointed out is that the OPs code is returning a pointer to a stack-based structure which has gone out of scope which is very likely to be corrupted by a timer or serial interrupt handler before the calling code even reads it....

Regards, Ray L.

This is not a problem and should not be flagged as such.