The scenario is that I have nested structure definition, and I have an array of 10 such structure. What I want is to initialize each of the element of both parent and the child structure with zero. Code is given below:
struct PressureValues
{
float SetPressure;
float ReadPressure;
};
struct CalibrationPoints
{
float SetTemperature;
struct PressureValues PressurePoints[10];
};
extern volatile struct CalibrationPoints code IntakeCalibrationPoints[10];
extern volatile struct CalibrationPoints code DischargeCalibrationPoints[10];
I know the lengthy method of initializing each structure element with zero using loop, but I am looking for a short method of initializing all elements to zero. Or what is the default initialize value of array of structure (containing floats only), is it zero or any random value?
volatile struct CalibrationPoints IntakeCalibrationPoints[10] = { { 0 } };
volatile struct CalibrationPoints DischargeCalibrationPoints[10] = { { 0 } };
This will initialise all elements to zero.
The reason that this works is that when you explicitly initialise at least one element of a data structure then all remaining elements which do not have initialisers specified will be initialised to zero by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With