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

问题描述

我正在研究使用 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 },};

    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 ) 类型函数的常量指针。即该函数具有返回类型 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 定义为没有参数并返回 eSystemState 的二维函数指针数组。

 static afEventHandler StateMachine =
    {
        [Idle_State] ={[Card_Insert_Event]= InsertCardHandler },};

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

 StateMachine[IdleState]

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

{[Card_Insert_Event]= InsertCardHandler }

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