c++ Initializing a struct with an array as a member

后端 未结 4 1307
旧巷少年郎
旧巷少年郎 2020-12-24 13:28

Edited again because it originally wasn\'t clear that I\'m trying to initialize the arrays at compile time, not at run time...


I\'ve got the following reduced

4条回答
  •  离开以前
    2020-12-24 13:53

    struct TestStruct {
        int length;
        const int *values;
    };
    
    static const uint8_t TEST_STRUCT_VAL_1[] = {0, 1, 2};
    const TestStruct t1 = {3, TEST_STRUCT_VAL_1};
    
    static const uint8_t TEST_STRUCT_VAL_2[] = {0, 1, 2, 3};
    const TestStruct t2 = {4, TEST_STRUCT_VAL_2};`
    

    In my case, stm32 programming with gcc/g++, the value array is only raw data storage, so it is static const, and would be stored in flash. I use this way to store font patterns for lcd display.

    using template is typing saving, but not size saving.

提交回复
热议问题