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():
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....
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.
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.