简体   繁体   English

函数指针数组指针用作函数的返回值

[英]Function Pointer Array Pointer is used as a function's return value

I want to create a function which returns a Function Pointer Array Pointer(ie a pointer points to an array whose elements are Function Pinters). 我想创建一个函数,该函数返回一个函数指针数组指针(即,一个指针指向其元素为函数指针的数组)。 I declared it like: 我这样声明:

void (*(*getFuncArrayPointer(int flag)))[3]{}

But I got errors: 但是我有错误:

returnFuncAttrPointer.c:27:10: error: declaration of ‘getFuncArrayPointer’ as array of voids
returnFuncAttrPointer.c:28: confused by earlier errors, bailing out
Preprocessed source stored into /tmp/ccYiPPru.out file, please attach this to your bugreport.

It seems that It parsed the array's element type is void. 似乎它解析的数组的元素类型为void。

Here is my whole code: 这是我的整个代码:

    #include <stdio.h>

    typedef void (*FUNC)(int);

    static void func1(int num)
    {
        printf("This is func1. num=%d\n", num + 1);
    }

    static void func2(int num)
    {
        printf("This is func2. num=%d\n", num + 2);
    }

    static void func3(int num)
    {
        printf("This is func3. num=%d\n", num + 3);
    }

    FUNC funcArray1[] = {func1, func2, func3};    // Function Pointer Array
    FUNC funcArray2[] = {func3, func2, func1};    // Function Pointer Array

    /* Function Pointer Array Pointer used as a return type */
    /* Fcuntion: getFuncArrayPointer(int flag) ==> func   Return type: void (*(*func))[3] ==> Array Pointer ==>
     * Array Pointer: Array--(*)[3] Elemnet--void (*func) ==> void (*funcArray(int start)) ==> Function Pointer
     */
    void (*(*getFuncArrayPointer(int flag)))[3]
    {
        switch(flag)
        {
            case 1:
                return &funcArray1;
            case 2:
                return &funcArray2;
            default:
                return NULL;
        }
    }

    int main()
    {
        int num;

        // (*pfuncArray)[3] ==> Array Pointer
        // void (*)(int) ==> Function Pointer
        void (*(*pfuncArray[3]))(int);  // Function Pointer Array Pointer

        printf("input a number: ");
        scanf("%d", &num);

        pfuncArray = getFuncArrayPointer(num);
if(pfuncArray)
    {
        (*funcArray)[0](num);
        (*funcArray)[1](num);
        (*funcArray)[2](num);
    }

    return 0;
}

GCC version: gcc (GCC) 4.6.2 20111027 (Red Hat 4.6.2-1) Copyright (C) 2011 Free Software Foundation, Inc. This is free software; GCC版本:gcc(GCC)4.6.2 20111027(Red Hat 4.6.2-1)版权所有(C)2011自由软件基金会,公司。 see the source for copying conditions. 请参阅复制条件的来源。 There is NO warranty; 没有保修; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 甚至不是出于适销性或针对特定目的的适用性。

OS: Linux sslvpn 3.6.10-2.fc16.i686.PAE #1 SMP Tue Dec 11 19:10:54 UTC 2012 i686 i686 i386 GNU/Linux 操作系统:Linux sslvpn 3.6.10-2.fc16.i686.PAE#1 SMP Tue Dec 11 11:10:54 UTC 2012 i686 i686 i386 GNU / Linux

void f1()
{
}

void f2()
{
}

void f3()
{
}


void (*func_arr[3])() = { f1, f2, f3 };

void (*(*getFuncArrayPointer(int flag))[3])()
{
  return &func_arr;
}

You could do it like this: 您可以这样做:

// create array of 3 function pointers
void (*function_array[3])() = {func1, func2, func3};

// function that gets that array and returns it
void (*(*getFParray(int num))[3])()
{
  return &function_array;
}

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

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