How to fix assignment of 'ptr to const' to 'ptr' compile error?

naseeam used Ask the Experts™
typedef struct
    SRV_Ntrfc_t          const  *ntrfc;
} SRV_Cnfg_t;

MGR_MR_E   srv_main(  MGR_MC_E    cmd,
                                       MGR_Work_t  *cmw,
                                       MGR_Cnfg_t  *cmc,
                                       MGR_Srv_t   *srv ) {

    Work_t   *wp     = srv->work;
    SRV_Cnfg_t   *cp     = srv->cnfg;
    SRV_Ntrfc_t  *ntrfc  = cp->ntrfc

Above code was compiled with WindRiver diab 4.2b 'C' compiler without errors.  Compiling this program with WindRiver diab 'C' compiler produces the following error:

trying to assign 'ptr to const' to 'ptr'

How to fix this error without changing software functionality?  Maybe some kind of typecast may do it.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can use const_cast<type>(), e.g.

ntrfc = const_cast<SRV_Ntrfc_t *>(cp->ntrfc);
Top Expert 2009

A const_cast should really only be a last resort (when there's no other way), because it's an ugly construct. Since you have control over the source code, you don't need a const_cast.

Just add a const here :

        const SRV_Ntrfc_t  *ntrfc  = cp->ntrfc;

or remove the const here :

        typedef struct
            SRV_Ntrfc_t            *ntrfc;
        } SRV_Cnfg_t;

depending on which makes more sense.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial