0

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

4
  • You can't initialise in the typedef. Anyway all your program is not written in the C. Commented Sep 11, 2017 at 14:05
  • 1
    This is all rather vague, but it would seem that you'd benefit from passing an array to the function, then flag which index that is the test. Let the function do the index calculation before accessing the element, something along the lines of void trace_test (size_t size, trace[size], size_t start) { for(size_t i=0; i<size; i++) { size_t index = (i + start) % size; trace[index] = whatever(); } Commented Sep 11, 2017 at 14:06
  • trace_test takes 6 arguments but you only supplied 1. Also you cannot assign to arrays. Commented Sep 11, 2017 at 14:58
  • Perhaps what you actually want is to not have not_trace_group at all, and write for (int i.......) trace_test(all_traces[i], all_traces[(i+1)%NUM_TRACES], ...... Commented Sep 11, 2017 at 15:00

1 Answer 1

1

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];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.