开发者

Designated dynamic array initialization

I need to find a solution to create an array and do designated initialization of the array during the declaration.

The following code works under the ARM11 C-compiler, but, as always, it does not work under the MSVC compiler. I'm trying to create a unit-test under Win32 to test the embedded SW before I deliver it to the customers.

The code looks like follows:

/* In some header file: */
...
/* Module1 */
#define P_proc1     0

/* Module2 */
#define P_proc2     ( P_proc1 + 1 )
#define P_proc3     ( P_proc2 + 1 )
#define P_proc4     ( P_proc3 + 1 )

/* Module3 */
#define P_proc5     ( P_proc4 + 1 )
#define P_proc6     ( P_proc5 + 1 )
#define P_proc7     ( P_proc6 + 1 )
#define Q_proc2_2nd_inst ( P_proc7 + 1 )
#define Q_proc3_2nd_inst ( Q_proc2_2nd_inst + 1 )
#define Q_proc5_2nd_inst ( Q_proc3_2nd_inst + 1 )
#define P_Last      ( Q_proc5_2nd_ins开发者_StackOverflow中文版t + 1 )
...
/* End of some header file */

And

/* The source file */
#include "some header file"
...
/* MACRO declarations */
/* For the following macro: PROC = base process name,
                            INSTANCE = base number of instance
                            GROUP = extended num of instances for this instance*/
#define PQ_ENTRY(PROC, INSTANCE, GROUP ) \
    [P_##PROC + INST - 1] = { queue_table + P_##PROC + INSTANCE - 1, \
                              queue_table + ( GROUP == 1 ? \
                              P_##PROC : \
                              Q_##PROC##_2nd_inst ) \
                              + INST - 1 }

/* GLOBAL VARIABLE declarations */
/* queue definitions for tasks */
t_queue queue_table[P_last + 1];

t_queue *const p_queue_table[][2] =  
{  
    PQ_ENTRY( proc3,    1, 2 ),  
    PQ_ENTRY( proc2,    1, 2 ),  
    PQ_ENTRY( proc7,    1, 1 ),  
    PQ_ENTRY( proc5,    1, 2 ),  
    PQ_ENTRY( proc6,    1, 1 ),  
    PQ_ENTRY( proc1,    1, 1 ),  
    PQ_ENTRY( proc4,    1, 1 )  
};  
...  
/* End of source file */  

The defines for P_proc* are also the index into the table (array) queue_table. For this example the process (base)names were made easy with only a few processes, but for the real code there are a lot more processes and the (base)names can be anything.

The idea of p_queue_table is to have a table which will hold the pointers into queue_table for the different instances of a process, and which has as index, yes, the process name def., P_procX.

It is not possible to rely on the order of entries in p_queue_table being in exactly the same order as the P_XXX process names are defined. If it was, then I would not need designated initialization of the table. One solution would be to just declare the p_queue_table array, and then initialize it later in a function, but that is not what is wanted.

It must be possible to extend/create macros to do the initialization of the table when the table is created, even if one needs to create another table which one can sort and use later for initialization of p_queue_table, ex of procs[][3] which is initialized with {{proc3, 1, 2}, {proc2, 1, 2}, ...}.

Does anyone have an answer for this problem?


your are using the designated initializer feature

double A[10] = { [1] = 32.0, [5] = 43.0 };

for array elements. This feature was introduced by C99, the valid standard for C since 12 years. AFAIK MSVC doesn't support C99 but only C89.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜