简体   繁体   中英

Is curly braces needed in initialization of an array of pointers in structure?

I have compiled a C code using Dev C++ and SPC5 Studio which has a structure with an array of pointers to another structure.

   typedef struct
    {
      uint16 Identifier_u16;
      void* const DataFncType;
    }ClassCfg_ts;

    typedef struct
    {
      uint32 Val_u32;
      ClassCfg_ts*  ClassRef_pu[2];
    }atrb_paramCfg_ts;

The initialisation is as given below

ClassCfg_ts ClassCfg1_s[] = {
{0,writeFucntion},//read write fucntion
{1,writeFucntion},//read write fucntion
{2,writeFucntion},//read write fucntion
};

ClassCfg_ts ClassCfg2_s[] = {
{0,writeFucntion},//read write fucntion
{1,writeFucntion},//read write fucntion
{2,writeFucntion},//read write fucntion
};

atrb_paramCfg_ts atrb_paramCfg_s[] =
{
{0,ClassCfg1_s},
{0,ClassCfg2_s},
};

where writeFunciton is defined. This initialization works perfectly whenDev C++ is used, whereas in SPC5 studio gives a warning for including curly braces and it disappears only if the initialisation is changed to

atrb_paramCfg_ts atrb_paramCfg_s[] =
{
{0,{ClassCfg1_s}},
{0,{ClassCfg2_s}},
};

Which among this is the correct initialisation?

ClassRef_p is an array of pointers to ClassCfg_ts . You need to brace initialize arrays. {ClassCfg2_s} is correct way to initialize it in the above snippet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM