这个 ```typedef``` 声明是什么?

What is this ```typedef``` declaration?

我正在研究使用 C 的状态机,我 运行 从 this site 进入这段代码。 有一些我以前从未见过的 typedef 声明:

    typedef eSystemState (*const afEventHandler[last_State][last_Event])(void); 

这里使用的是:

    // Table to define valid states and event of finite state machine
    static afEventHandler StateMachine =
    {
        [Idle_State] ={[Card_Insert_Event]= InsertCardHandler },
        [Card_Inserted_State] ={[Pin_Enter_Event] = EnterPinHandler },
        [Pin_Eentered_State] ={[Option_Selection_Event] = OptionSelectionHandler},
        [Option_Selected_State] ={[Amount_Enter_Event] = EnterAmountHandler},
        [Amount_Entered_State] ={[Amount_Dispatch_Event] = AmountDispatchHandler},
    };

有人可以解释一下这是什么吗?这是完整的代码

#include <stdio.h>

//Different state of ATM machine
typedef enum
{
    Idle_State,
    Card_Inserted_State,
    Pin_Eentered_State,
    Option_Selected_State,
    Amount_Entered_State,
    last_State
} eSystemState;

//Different type events
typedef enum
{
    Card_Insert_Event,
    Pin_Enter_Event,
    Option_Selection_Event,
    Amount_Enter_Event,
    Amount_Dispatch_Event,
    last_Event
} eSystemEvent;

//typedef of 2d array
typedef eSystemState (*const afEventHandler[last_State][last_Event])(void);

//typedef of function pointer
typedef eSystemState (*pfEventHandler)(void);

//function call to dispatch the amount and return the ideal state
eSystemState AmountDispatchHandler(void)
{
    return Idle_State;
}

//function call to Enter amount and return amount enetered state
eSystemState EnterAmountHandler(void)
{
    return Amount_Entered_State;
}

//function call to option select and return the option selected state
eSystemState OptionSelectionHandler(void)
{
    return Option_Selected_State;
}

//function call to enter the pin and return pin entered state
eSystemState EnterPinHandler(void)
{
    return Pin_Eentered_State;
}

//function call to processing track data and return card inserted state
eSystemState InsertCardHandler(void)
{
    return Card_Inserted_State;
}

int main(int argc, char *argv[])
{
    eSystemState eNextState = Idle_State;
    eSystemEvent eNewEvent;

// Table to define valid states and event of finite state machine
    static afEventHandler StateMachine =
    {
        [Idle_State] ={[Card_Insert_Event]= InsertCardHandler },
        [Card_Inserted_State] ={[Pin_Enter_Event] = EnterPinHandler },
        [Pin_Eentered_State] ={[Option_Selection_Event] = OptionSelectionHandler},
        [Option_Selected_State] ={[Amount_Enter_Event] = EnterAmountHandler},
        [Amount_Entered_State] ={[Amount_Dispatch_Event] = AmountDispatchHandler},
    };

    while(1)
    {
        // assume api to read the next event
        eSystemEvent eNewEvent = ReadEvent();
        //Check NULL pointer and array boundary
        if( ( eNextState < last_State) && (eNewEvent < last_Event) && StateMachine[eNextState][eNewEvent]!= NULL)
        {
            // function call as per the state and event and return the next state of the finite state machine
            eNextState = (*StateMachine[eNextState][eNewEvent])();
        }
        else
        {
            //Invalid
        }
    }
    return 0;
}

让我们考虑一下 typedef

typedef eSystemState (*const afEventHandler[last_State][last_Event])(void);

循序渐进。

这个

afEventHandler[last_State][last_Event]

声明一个二维数组。考虑到枚举数 last_StatelastEvent 等于 5 那么上面的记录就等同于

afEventHandler[5][5]

这个

*const afEventHandler[last_State][last_Event]

声明一个二维常量指针数组(指针本身是常量)。

最后这个

typedef eSystemState (*const afEventHandler[last_State][last_Event])(void);

声明了一个二维数组,常量指针指向eSystemState ( void )类型的函数。即函数具有 return 类型 eSystemState 和参数列表 void.

因此名称 afEventHandler 是指向 eSystemState ( void )

类型函数的二维常量指针数组类型的同义词

至于这个声明

// Table to define valid states and event of finite state machine
static afEventHandler StateMachine =
{
    [Idle_State] ={[Card_Insert_Event]= InsertCardHandler },
    [Card_Inserted_State] ={[Pin_Enter_Event] = EnterPinHandler },
    [Pin_Eentered_State] ={[Option_Selection_Event] = OptionSelectionHandler},
    [Option_Selected_State] ={[Amount_Enter_Event] = EnterAmountHandler},
    [Amount_Entered_State] ={[Amount_Dispatch_Event] = AmountDispatchHandler},
};

然后使用指定的初始化程序声明并初始化上述类型的数组。

这里是一个使用枚举的一维函数指针数组的类似 typedef 声明的更简化的示例。

#include <stdio.h>

typedef enum { Add, Subtract, Multiply, Divide, TotalOperations } Operation;

typedef int ( * const Action[TotalOperations] )( int, int );

int add( int x, int y )      { return x + y; }
int subtract( int x, int y ) { return x - y; }
int multiply( int x, int y ) { return x * y; }
int divide( int x, int y )   { return x / y; }

int main(void) 
{
    Action action =
    {
        [Add] = add, [Subtract] = subtract, [Multiply] = multiply, [Divide] = divide
    };
    
    int x = 100, y = 5;
    
    for ( int i = 0; i < TotalOperations; i++ )
    {
        printf( "%d\n", action[i]( x, y ) );
    }
    
    return 0;
}

程序输出为

105
95
500
20
typedef eSystemState (*const afEventHandler[last_State][last_Event])(void);

afEventHandler 定义为没有参数的函数指针的二维数组和 returns 一个 eSystemState.

 static afEventHandler StateMachine =
    {
        [Idle_State] ={[Card_Insert_Event]= InsertCardHandler },
        [Card_Inserted_State] ={[Pin_Enter_Event] = EnterPinHandler },
        [Pin_Eentered_State] ={[Option_Selection_Event] = OptionSelectionHandler},
        [Option_Selected_State] ={[Amount_Enter_Event] = EnterAmountHandler},
        [Amount_Entered_State] ={[Amount_Dispatch_Event] = AmountDispatchHandler},
    };

是定义类型的变量StateMachine的初始化。

 StateMachine[IdleState]

然后被初始化为一维指针数组,指向无参数的函数,返回 eSystemState 和:

{[Card_Insert_Event]= InsertCardHandler }

一维数组,其索引 Card_Insert_Event 的元素对应于函数 InsertCardHandler.