简体   繁体   English

函数从表返回函数指针作为参数

[英]Function returning function pointer from table as a parameter

I have been reading for a while, but today I can't figure someting out and find a solution. 我已经阅读了一段时间,但是今天我无法解决问题并找到解决方案。

How to return a function pointer from a function table as parameter? 如何从功能表返回功能指针作为参数? All similair solutions don't work for this one and end up not compiling. 所有similair解决方案均不适用于该解决方案,并且最终无法编译。

I have tried a lot of methods but the compiler always returns with errors like: 我尝试了很多方法,但是编译器总是返回以下错误:

function returning function is not allowed solution (when using typedef void (*func)(); ) 函数不允许返回函数的解决方案(使用typedef void (*func)();

As NO parameters have to be passed into the final routine it should be possible. 由于必须将NO参数传递到最终例程中,因此应该可行。

My simplified example: 我的简化示例:

void PrintOne(void) { printf("One")};
void PrintTwo(void) { printf("Two")};

struct ScanListStruct
{
    int Value;
    void (*Routine)(void);
}

const ScanListStruct DoList[] =
{ 
    {1, PrintOne},
    {2, PrintTwo}
}

bool GetRoutine(void *Ptr, int Nr)
{
    for (int x =0; x<=1; x++)
    {
        if (DoList[x].Value = Nr)   
        {
            Ptr = DoList[(x)].Routine;
            //((*DoList[(x)].Routine)());    // Original Working and executing version!
            return true;
        }
    }
    return false;
}

void main(void)
{
    int y = 1;
    void (*RoutineInMain)();     // Define
    if (GetRoutine( RoutineInMain, y) == true)    // get the address
    {   
        RoutineInMain();    // Execute the function
    }
}

There a few things wrong with the code; 代码有些错误;

  • Syntax errors (missing ; etc.) 语法错误(缺少;等)
  • main must return int main必须返回int
  • GetRoutine should accept the function pointer by reference, not just a void* pointer to anything GetRoutine应该通过引用接受函数指针,而不仅仅是指向任何对象的void*指针
  • if condition should contain an equality test, not an assignment if条件应该包含一个平等的测试,而不是分配

As follows, works as expected; 如下所述,按预期方式工作;

void PrintOne(void) { printf("One"); };
void PrintTwo(void) { printf("Two"); };

struct ScanListStruct
{
    int Value;
    void (*Routine)(void);
};

const ScanListStruct DoList[] =
{ 
    {1, &PrintOne},
    {2, &PrintTwo}
};

bool GetRoutine(void (*&Ptr)(), int Nr)
{
    for (int x =0; x<=1; x++)
    {
        if (DoList[x].Value == Nr)   
        {
            Ptr = *DoList[(x)].Routine;
            //((*DoList[(x)].Routine)());    // Original Working and executing version!
            return true;
        }
    }
    return false;
}

int main(void)
{
    int y = 1;
    void (*RoutineInMain)();     // Define
    if (GetRoutine( RoutineInMain, y) == true)    // get the address
    {   
        RoutineInMain();    // Execute the function
    }
}

Prints One . 打印One

You have lots of errors in your code. 您的代码中有很多错误。 Like here you put the comas at the wrong place: 像这里,您将昏迷放在错误的位置:

void PrintOne(void) { printf("One")};
void PrintTwo(void) { printf("Two")};

It should be 它应该是

void PrintOne(void) { printf("One");}
void PrintTwo(void) { printf("Two");}

And here you are using the wrong operator, = instead of ==. 在这里,您使用了错误的运算符,而不是==。

if (DoList[x].Value = Nr) 

When the argument Ptr is a pointer, and that is passed by value, so the value assigned in the function will not be available when the function returns. 当参数Ptr是一个指针,并且由值传递时,因此在函数返回时,在函数中分配的值将不可用。

This is how your code should be: 这是您的代码应为:

void PrintOne(void) { printf("One"); }
void PrintTwo(void) { printf("Two"); }

typedef void(*prototype)();

struct ScanListStruct
{
   int Value;
   prototype Routine;
};

const ScanListStruct DoList[] =
{
   { 1, PrintOne },
   { 2, PrintTwo }
};

bool GetRoutine(prototype &Ptr, int Nr)
{
   for (int x = 0; x <= 1; x++)
   {
      if (DoList[x].Value == Nr)
      {
         Ptr = DoList[(x)].Routine;
         return true;
      }
   }
   return false;
}

int main()
{
   int y = 1;
   prototype RoutineInMain;     // Define
   if (GetRoutine(RoutineInMain, y) == true)    // get the address
   {
      RoutineInMain();    // Execute the function
   }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM