简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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