C/C++---C and CXX compiler with c or cpp in struct
2017-01-05 22:23
459 查看
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++编译,那么需要对结构体的初始化要考虑周到。这里建议采用第二种或者
第三种方式,并操持按变量顺序来初始化。
相关文章推荐
- Polymorphism in Perl comparing with JAVA and C++
- When does compiler create default and copy constructors in C++?
- Using UTF-8 as the internal representation for strings in C and C++ with Visual Studio
- Difference in the result compiled by G++ and Microsoft cpp compiler.
- Maven打包异常Unable to locate the Javac Compiler in:Please ensure you are using JDK 1.4 or above and
- What are the differences between struct and class in C++?
- Command Line Applications with Visual Studio 2008 and C++ Posted by omniplex in Errors
- Difference between CPPFLAGS and CXXFLAGS in GNU Make
- Difference between 'struct' and 'typedef struct' in C++?
- SpringData JPA @Query分页查询 Cannot use native queries with dynamic sorting and/or pagination in method
- [C/CPP系列知识] Type difference of character literals 和 bool in C and C++
- Ch3-4: solve Hanoi in C++ with recursion, and with stack explicitly
- Apache/Tomcat with Failover and Load Balancing in 20 minutes or less…
- Code completion (IntelliSense) for C++ in Vim with OmniCppComplete
- How to do run-time (or explicit) linking of C++ plug-in components and objects
- spring.data.jpa.query :Cannot use native queries with dynamic sorting and/or pagination in method
- How-To : Using EJB 3.0 and Java Persistence API with Spring in OC4J
- Visual C++ ActiveX Control for hosting Office documents in Visual Basic or HTML
- Introduction to Programming in C/C++ with Vim
- [引用]SQL Server 2005 Books Online How to: Create a Job with Steps and a Schedule in Visual Basic .NET