开发者

Can't initialize struct with nested pointers to other structs

开发者 https://www.devze.com 2023-04-10 00:23 出处:网络
I\'m using a third party library which defines this struct: typedef struct { unsigned short nbDetectors;

I'm using a third party library which defines this struct:

typedef struct
{
    unsigned short nbDetectors;
    //! structure of detector status
    struct DetectorStatus
    {
        unsigned int lastError;         //< last detector internal error
        float temperature;              //< detector temperature
        detector_state state;           //< detector state
        unsigned short mode;            //< detector mode

        struct EnergyStatus
        {
            power_source powerSource;           //< front-end power source
            frontend_position frontendPosition; //< front-end position relative to the docking station

            struct BatteryStatus
            {
                bool present;                   //< battery present or not in the detector
                unsigned short charge;          //< charge level of the battery (in %)
                float voltageLevel;             //< battery voltage level
                float temperature;              //< temperature of the battery
                unsigned short chargeCycles;    //< number of charge/discharge cycles
                unsigned short accuracy;        //< Expected accuracy for charge level (in %)
                bool needCalibration;

            } batteryStatus;

        } * energyStatus;

        struct GridStatus
        {
            detector_grid grid;

        } * gridStatus;

    } * dete开发者_开发知识库ctorStatus;

} HardwareStatus;

This struct is used by the library as data passed by one of its callbacks. So it's the library which fills it up, I just read it. So far, so good.

But now I'm writing an emulator for the device handled by this library, so now I have to fill up one of these structs and I can't get it right.

I tried this:

HardwareStatus status;
status.detectorStatus->temperature = 20 + rand() % 10;
e.data = &status;
m_pContext->EventCallback( EVT_HARDWARE_STATUS, &e );

When I compiled, I got:

warning C4700: uninitialized local variable 'status' used

Then I realized... The pointers inside the struct are pointing to garbage, nice catch Visual Studio! So then I tried to start by declaring an instance of the innermost struct (BatteryStatus), but that wouldn't compile... because it's not typedef'd (it says the BatteryStatus type is not defined)? So I got stumped... How do I fill the struct up?


if you want to have everything on the stack this should do it:

// Getting structs on the stack initialized to zero
HardwareStatus status = { 0 };
HardwareStatus::DetectorStatus detectorStatus = { 0 };
HardwareStatus::DetectorStatus::EnergyStatus energyStatus = { 0 };
HardwareStatus::DetectorStatus::GridStatus gridStatus = { 0 };

// "Linking" structs
detectorStatus.energyStatus = &energyStatus;
detectorStatus.gridStatus = &gridStatus;
status.detectorStatus = &detectorStatus;

// Now you can fill and use them
status.detectorStatus->temperature = 20 + 3 % 10;
//...


You could value-iniitialize it:

HardwareStatus status = {};

If you want to instantiate a BatteryStatus, you can do that by fully qualifying the name:

HardwareStatus::DetectorStatus::EnergyStatus::BatteryStatus bs;


did you try memset'ing the struct to 0 ?

0

精彩评论

暂无评论...
验证码 换一张
取 消