C/C++---C and CXX compiler with c or cpp in struct

来源:互联网 发布:上海市软件协会 编辑:程序博客网 时间:2024/06/05 08:42

C/C++中结构体初始化方法

#include <stdio.h>enum SIZE {    SIZE_ONE    = 1,    SIZE_TWO    = 2,    SIZE_THREE  = 3};struct DATA{    int         data1;    char        data2;    enum SIZE   size;};struct STRUCT_INIT_TEST{    int     num1;    char    *str1;    char    *str2;    int     num2;};int main(){    // 初始化方式一,C/C++通用    struct STRUCT_INIT_TEST test1 = {2, "string1", "string2", 4};    // 初始化方式二,可以乱序,但是只能以.c结尾并用gcc编译,但是按变量顺序初始化,可以用g++编译通过    struct STRUCT_INIT_TEST test2 = {        .num1 = 2,        .str2 = "string2",        .str1 = "string1",        .num2 = 4    };    // 初始化方式三,可以乱序,但是按变量顺序来初始化,gcc/g++均能够编译以.c或.cpp的文件    struct STRUCT_INIT_TEST test3 = {        num1 : 2,        str1 : "string1",        str2 : "string2",        num2 : 4    };    return 0;}

总结:

  • 方式一:按结构体变量顺序初始化相应变量,在变量的前面不能有未初始化的变量,而后面允许有变量未初始化。
  • 方式二,可以乱序,但是只能以.c结尾并用gcc编译,但是按变量顺序初始化,可以用g++编译通过
  • 方式三,可以乱序,但是如果是对于结构体中的每个成员变量都按顺序初始化,那么gcc/g++对于.c或者.cpp均能够编译通过(在gcc4.6.x和gcc4.9.x上测试通过)
// 对于联合体的情况下:typedef struct{    COMBO_DEV       devno;              /* device number, select sensor0 and sensor 1 */    input_mode_t    input_mode;         /* input mode: MIPI/LVDS/SUBLVDS/HISPI/DC */    phy_clk_share_e phy_clk_share;    img_rect_t      img_rect;           /* MIPI Rx device crop area (corresponding to the oringnal sensor input image size) */    union    {        mipi_dev_attr_t     mipi_attr;        lvds_dev_attr_t     lvds_attr;    };} combo_dev_attr_t;// 初始化方法:combo_dev_attr_t SUBLVDS_4lane_SENSOR_MN34220_12BIT_1080_NOWDR_ATTR ={    .devno         = 0,    .input_mode    = INPUT_MODE_SUBLVDS,                /* input mode */    .phy_clk_share = PHY_CLK_SHARE_NONE,    .img_rect = {0, 0, 1920, 1108},    {   // 此处需要加‘{’,否则只能用gcc编译通过        .lvds_attr = {            .raw_data_type    = RAW_DATA_12BIT,            .wdr_mode         = HI_WDR_MODE_NONE,            .sync_mode        = LVDS_SYNC_MODE_SOF,            .vsync_type       = {LVDS_VSYNC_NORMAL, 0, 0},            .fid_type         = {LVDS_FID_NONE, HI_TRUE},            .data_endian      = LVDS_ENDIAN_BIG,            .sync_code_endian = LVDS_ENDIAN_BIG,            .lane_id = {0, 2, -1, -1, 1, 3, -1, -1, -1, -1, -1, -1},            .sync_code = {                {   {0x002, 0x003, 0x000, 0x001},      // lane 0                    {0x002, 0x003, 0x000, 0x001},                    {0x002, 0x003, 0x000, 0x001},                    {0x002, 0x003, 0x000, 0x001}                },                {                    {0x012, 0x013, 0x010, 0x011},      // lane 1                    {0x012, 0x013, 0x010, 0x011},                    {0x012, 0x013, 0x010, 0x011},                    {0x012, 0x013, 0x010, 0x011}                },                {                    {0x006, 0x007, 0x004, 0x005},      // lane2                    {0x006, 0x007, 0x004, 0x005},                    {0x006, 0x007, 0x004, 0x005},                    {0x006, 0x007, 0x004, 0x005}                },                {                    {0x016, 0x017, 0x014, 0x015},      // lane3                    {0x016, 0x017, 0x014, 0x015},                    {0x016, 0x017, 0x014, 0x015},                    {0x016, 0x017, 0x014, 0x015}                }            }        }    }};

  如上面,联合体的初始化需要在用g++的编译中加上花括号。

  进一步总结,在程序设计中,考虑到代码的可兼容性和可移植性(低版本编译器),切换使用gcc/g++编译,那么需要对结构体的初始化要考虑周到。这里建议采用第二种或者第三种方式,并操持按变量顺序来初始化。

0 0
原创粉丝点击