Home > Software design >  Invoking function from the 2d array of function pointers
Invoking function from the 2d array of function pointers

Time:08-24

#include <stdio.h>

void test1(){ printf("test1\n"); }
void test2(){ printf("test2\n"); }
void test3(){ printf("test3\n"); }
void test4(){ printf("test4\n"); }

void (*hv_ptr[2])() = {test1,test2};
void (*dev_ptr[2])() = {test3,test4};

void (**ptr[2])() = {hv_ptr, dev_ptr};

int main() {

   ptr[0][0];
   ptr[0][1];
   ptr[1][0];
   ptr[1][1];

   return 0;
}

I have declared the array of pointers to function pointers. But with this code, functions are not getting called. Kindly let me know what is going wrong here.

CodePudding user response:

ptr is an array of pointers to pointers to functions, so ptr[0] is a pointer to a pointer to a function, so ptr[0][0] is a pointer to a function.

Thus, in the expression statement ptr[0][0];, the expression is just a pointer to a function. It does not contain a function call.

ptr[0][0](); is a call to the function pointed to by ptr[0][0].

  • Related