함수포인터 배열을 제가 이해 못 하는 것 같습니다.

study의 이미지

오랫만에 질문을 올리네요.

최근에 State Machine에 대해서 여기저기 자료를 보고 있다가,발견한 곳인데요. https://yakking.branchable.com/posts/state-machines-in-c/
State Machine을 간단하게 이해하기 좋게 설명해 놓은 것 같아서 보다가

아래의 event handler 부분에서 이해를 못해서 시간을 보내고 있습니다.

event_handler transitions[STOP_LOOPING+1][END+1] = {
    [START] = { [START_LOOPING] = start_looping, },
    [LOOP] = { [PRINT_HELLO] = print_hello,
               [STOP_LOOPING] = stop_looping, },
};
<code>
 
여기서 "[START] =" 라거나, "[START_LOOPING] =" 는 어떻게 이해해야하는 건가요?

라스코니의 이미지

매크로 입니다.

void start_looping() { ... }
void print_hello() { ... }
void stop_looping() { ... }
 
#define START 0
#define END 1
#define START_LOOPING 0
#define LOOP  1
#define PRINT_HELLO 0
#define STOP_LOOPING 1
 
event_handler transitions[2][2] = {
    [0] = { [0] = start_looping, },
    [1] = { [0] = print_hello,
               [1] = stop_looping, },
};

study의 이미지

그러니까, [0] = { [0] = start_looping }이라고 하면, 배열의 [0][0]만 초기화를 하고, [0][1], [0][2]는 초기화하지 않는 다는 말인가 보네요.

이렇게 필요한 부분만 초기화를 해도 안전한건가요?
예를 들면, 명시적으로 초기화하지 않는 [0][1], [0][2]에 이상한 값이 들어갈 걱정은 안해도 되는건지요?

라스코니의 이미지

없는 부분은 아마 0(NUL)이 들어가게 될 것임으로 사용할 때 한번 체크가 필요할 겁니다.

if(func_ptr)
   (*func_ptr)();

이런 식으로요.