简体   繁体   中英

Populate an array of array of structs?

I want to declare a struct, then an array of those structs, then an array of an array of those structs. The first two parts are fine, I'm struggling with the third.

When I try to assign an array to not_trace_groups, My code will not compile and I can't figure out how to correctly do this.

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
}

The error I receive is:

 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])};

I thought perhaps I can't assignto the second level of an array so I tried this:

trace not_trace_groups[NUM_TRACES];

but this gives the same error

You cannot use -

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])};

As this is assignment which can be used at the time of initialization only.You cannot assign value to not_trace_groups[i] like this. All you need to do is use 2 for loops and get exact element not_trace_group[i][j] and assign value to it. For example, here you can assign value like -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM