c-这个``typedef```声明是什么



我在研究使用C的状态机,我在这个网站上看到了这段代码。有一些我以前从未见过的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);

声明了指向类型为CCD_ 5的函数的常量指针的二维数组。也就是说,函数具有返回类型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( "%dn", action[i]( x, y ) );
    }
    
    return 0;
}

程序输出为

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

将CCD_ 10定义为没有参数的函数指针的2D阵列,并返回CCD_。

 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},
    };

是所定义类型的变量CCD_ 12的初始化。

 StateMachine[IdleState]

然后初始化为指向函数无参数的指针的1D数组,返回带有:的eSystemState

{[Card_Insert_Event]= InsertCardHandler }

其索引CCD_ 14的元素对应于函数CCD_。

相关内容

  • 没有找到相关文章

最新更新