我有一个鸡生蛋的问题。我想将一个数据结构传递给一个例程,该结构包含一个指向需要该结构的例程的指针。
我制作了一个非常简单的示例。
如果我在结构体后面添加CalcDataStruct,那么FuntctionPrototype未定义。
我遇到的问题只存在于前两行,其余部分可能包含一些语法错误,因为我还没有在编译器内检查过。
typedef void(*FunctionPrototype)(CalcDataStruct *Ptr);
struct CalcDataStruct
{
int A, B, C, D;
int Values;
char SignA, Sign B;
int Result;
FunctionPrototype Routine;
}
struct ScanStruct
{
char Sign;
int Values;
FunctionPrototype Routine;
};
const ScanStruct ExampleList[] =
{
{ '+', 2, AddTwo },
{ '+', 3, AddThree }
};
void AddTwo(CalcDataStruct *Ptr)
{
// use the data and if needed put it back
}
void AddThree(CalcDataStruct *Ptr)
{
// use the data and if needed put it back
}
void GetFunction(CalcDataStuct *Ptr, ScanStruct *List)
{
// Very simple return based on nothing
Ptr->Routine = *List[(1)].Routine;
}
void main()
{
CalcDataStruct A;
// struct is filled
// Fill in the routine pointer based on data
GetFunction(A, ExampleList)
// Execute the routine fetched with all the data
A->Routine(A)
}