2017-09-11 7 views
0

구조체를 선언 한 다음 해당 구조체의 배열을 선언 한 다음 해당 구조체 배열의 배열을 선언하려고합니다. 첫 번째 두 부분은 괜찮습니다. 세 번째 부분과 고민하고 있습니다.구조체의 배열 배열 채우기?

배열을 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 
} 

I 수신 오류 :

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는 I 배열의 두 번째 레벨 그래서이 시도 할 수 assignto 아마도 생각

:

trace not_trace_groups[NUM_TRACES]; 

을하지만 이는 범 같은 오류

+0

typedef에서 초기화 할 수 없습니다. 어쨌든 모든 프로그램은 C로 작성되지 않습니다. –

+1

이것은 다소 모호 합니다만, 함수에 배열을 건네주는 것이 도움이되는 것처럼 보일 것입니다. (size_t i = 0; i Lundin

+0

'trace_test'는 6 개의 인수를 취하지 만 1 개만 제공했습니다. 또한 배열에 할당 할 수 없습니다. –

답변

1

사용할 수 없습니다. -

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 for 루프를 사용하고 정확한 요소 not_trace_group [i] [j]을 가져 와서 값을 할당하기 만하면됩니다. 예를 들어, 여기에 할당 할 수있는 값은 같은 -

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

당신은 할당 식을 사용하여 배열을 초기화하려고 노력하지만, 오른쪽에 당신은 배열 이니셜 라이저 {a,b,c, ...}를 사용합니다. 구조체를 초기화하는이 방법은 선언 후에 만 ​​허용됩니다 struct_type struct_var = {init...}.