As I alluded to in a previous thread, I am trying to up my C skill level and write more efficient and robust code. I have run into another question. Let's say I am using my ADCs to measure three quantities. I've defined a struct to neatly wrap them up:
In main(), I create two instances of this struct: one to contain the raw results from the ADC, the other to contain the calibrated measurements which have been shifted and scaled:
I want to write a function to take the raw measurements and do the shifting and scaling, by which point "latest_measurements" should contain the correctly calibrated values. The most obvious solution seemed to be to pass "raw_measurements" into a function, create a temporary variable to hold the results then return it at the end:
Then in main(), I assign this to the "latest_measurements" variable:
Question: Is this a good and/or standard approach? I am thinking now that I could avoid the overhead of creating this temporary variable by passing two pointers as arguments: a pointer to the "raw_measurements" and a pointer to the "latest_measurements". What would be the pros and cons of this? I know that @WBahn mentioned in a previous thread that there would be "some access overhead and that might be too much" so perhaps could elaborate on this?
Code:
typedef struct measurements
{
float i_a;
float i_b;
float i_c;
} measurements;
Code:
measurements raw_measurements; // Contains the raw ADC conversion results
measurements latest_measurements; // Contains the shifted and scaled measurements
Code:
measurements calibrate_measurements(measurements raw)
{
measurements temp;
temp.i_a = (raw.i_a - CURRENT_A_OFFSET) * CURRENT_A_GAIN;
temp.i_b = (raw.i_b - CURRENT_B_OFFSET) * CURRENT_B_GAIN;
temp.i_c = (raw.i_c - CURRENT_C_OFFSET) * CURRENT_C_GAIN;
return temp;
}
Code:
latest_measurements = calibrate_measurements(raw_measurements);