1

on this follow program I've tried to make a function that returns a pointer to array of 4 function pointers but I don't know the way how it works and how can I call it and receive the return value also there is an array of 4 function pointers (made it to extract the difference but I Couldn't) now I'm confused of the way that I can call the functions from any 2 of these methods whether array or the Function.

#include <stdio.h>

uint32 (*(*Fun2Ptr(void))[4])(void);
uint32 Function_1(void);
uint32 Function_2(void);
uint32 Function_3(void);
uint32 Function_4(void);

uint32(*Arrayof4FP[4])(void) = {Function_1 ,Function_2 ,
                                Function_3 ,Function_4};

int main()
{
    uint32(*(*LocalMainArrof4FP)[4])(void) = Fun2Ptr();
    printf("---------------Array--------------\n");
    printf("%i \n" , (*Arrayof4FP[0])()); // 1
    printf("%i \n" , (*Arrayof4FP[1])()); // 2
    printf("%i \n" , (*Arrayof4FP[2])()); // 3
    printf("%i \n" , (*Arrayof4FP[3])()); // 4
    printf("-----------Using Function-------------\n");
    printf("%i \n" ,(*LocalMainArrof4FP)[0]() ); // 1
    printf("%i \n" ,(*LocalMainArrof4FP)[1]() ); // 2
    printf("%i \n" ,(*LocalMainArrof4FP)[2]() ); // 3
    printf("%i \n" ,(*LocalMainArrof4FP)[3]() ); // 4
    
    return 0;
}
uint32 Function_1(void)
{
    return 1;
}
uint32 Function_2(void)
{
    return 2;
}
uint32 Function_3(void)
{
    return 3;
}
uint32 Function_4(void)
{
    return 4;
}
uint32 (*(*Fun2Ptr(void))[4])(void)
{
    uint32 (*(*LocalPtrs)[4])(void) = &Arrayof4FP;
    return LocalPtrs;
}
4
  • To make it much simpler, use type-aliases. Like typedef uint32 (*funcptr)(void);. Then you can use it as any other type, like funcptr* Fun2Ptr(void); or funptr Arrayof4FP[] = { ... }; Commented Nov 26, 2023 at 11:18
  • Also note that arrays decay to pointers to their first element, and that is usually enough to handle arrays. So instead of e.g. funcptr (*localptrs)[4] = &Arrayof4FP;, you can just do funcptr *localptrs = Arrayof4FP; /* Same as &Arrayof4FP[0] */ Then you can just use localptrs[i] like any other array. Commented Nov 26, 2023 at 11:20
  • You need a typedef or include for uint32. Commented Nov 26, 2023 at 11:47
  • Similarly, functions decay into function pointers, so you can de-reference a function pointer as much as you want, it will only bounce back from function to function pointer again. Commented Nov 28, 2023 at 15:19

1 Answer 1

2

You can't return an array from a function instead return a pointer. Moved the array to the function and ensure liveliness with the static keyword. Added the missing typedef for uint32, and used the format macros from inittypes.h to ensure the correct size:

#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>

typedef uint32_t uint32;

uint32 Function_1(void) {
    return 1;
}

uint32 Function_2(void) {
    return 2;
}

uint32 (**Fun2Ptr(void))(void) {
    static uint32 (*fp[])(void) = { Function_1, Function_2 };
    return fp;
}

int main(void) {
    uint32 (**fp)(void) = Fun2Ptr();
    printf("-----------Using Function-------------\n");
    printf("%" PRIu32 "\n", fp[0]()); // 1
    printf("%" PRIu32 "\n", fp[1]()); // 2
    return 0;
}

and resulting output:

-----------Using Function-------------
1
2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.