簡體   English   中英

填充結構數組的數組?

[英]Populate an array of array of structs?

我想聲明一個結構,然后聲明這些結構的數組,然后聲明這些結構的數組。 前兩個部分很好,我正在努力地進行第三部分。

當我嘗試將數組分配給not_trace_groups時,我的代碼將無法編譯,並且無法弄清楚如何正確執行此操作。

typedef struct trace
{
    uint32_t upstream_pin;
    uint32_t dnstream_pin;
    uint32_t led_pin;
}trace;

trace all_traces[NUM_TRACES] = {{GPIO_Pin_3, GPIO_Pin_7, GPIO_Pin_0},
                            {GPIO_Pin_4, GPIO_Pin_6 ,  GPIO_Pin_1},
                            {GPIO_Pin_5,  GPIO_Pin_5 , PIO_Pin_1},
                            {GPIO_Pin_6,  GPIO_Pin_4 , PIO_Pin_0},
                            {GPIO_Pin_7,  GPIO_Pin_3 , GPIO_Pin_1},
                            {GPIO_Pin_0,  GPIO_Pin_15, GPIO_Pin_2}};

trace not_trace_groups[NUM_TRACES][NUM_TRACES];

void main()
{
    for (int i = 0; i < NUM_TRACES; i++)
    {
        not_trace_group[i] = {all_traces[i], all_traces[(i+1)%NUM_TRACES], all_traces[(i+2)%NUM_TRACES], all_traces[(i+3)%NUM_TRACES], all_traces[(i+4)%NUM_TRACES], all_traces[(i+5)%NUM_TRACES])};
    }
    while(1 == 1){
        for (int i = 0; i < NUM_TRACES; i++)
        {
            trace_test(not_trace_group[i]);
        }
}

void trace_test(trace cur_trace, trace not_trace1, trace not_trace2, trace not_trace3, trace not_trace4, trace not_trace5)
{
 // do some stuff
}

我收到的錯誤是:

 error: expected expression before '{' token
         not_trace_group[i] = {all_traces[i], all_traces[(i+1)%NUM_TRACES], all_traces[(i+2)%NUM_TRACES], all_traces[(i+3)%NUM_TRACES], all_traces[(i+4)%NUM_TRACES], all_traces[(i+5)%NUM_TRACES])};

我以為也許我不能分配給數組的第二層,所以我嘗試了一下:

trace not_trace_groups[NUM_TRACES];

但這給出了相同的錯誤

您不能使用-

not_trace_group[i] = {all_traces[i], all_traces[(i+1)%NUM_TRACES], all_traces[(i+2)%NUM_TRACES], all_traces[(i+3)%NUM_TRACES], all_traces[(i+4)%NUM_TRACES], all_traces[(i+5)%NUM_TRACES])};

因為這是只能在初始化時使用的分配。您不能像這樣將值分配給not_trace_groups [i]。 您需要做的就是使用2進行循環,並獲取確切的元素not_trace_group [i] [j]並為其分配值。 例如,您可以在此處分配值-

not_trace_group[0][0] = all_traces[0];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM