[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

Pointer to Functions ?

Hello,
 I have question regarding pointer to functions ? I have written the piece of code just below and my query follows after that.

------------Start of Code --------------
// a function definition
void x(void (*pointfunction)(void))
{
  void *ptr = pointfunction;
}

// Call made to x
void foo()
{
// My pointer to the function
void (*pointfunc[3])(void) =
{
 func1,func2,func3
};

  for (int i =0; i<3;i++)
    x(pointfunc[i]);
}
void func1()
{
   printf("Hello, world -- 1");
}
void func2()
{
   printf("Hello, world -- 2");
}
void func3()
{
   printf("Hello, world -- 3");
}
------------------End of code---------
In the above piece code, in the function "x()" how can "func1" or "func2" or "func3" be called using the the "void *ptr". Detailed explanation would be appreciated.
Note: I am using VC++. I dont have any compiler problems.

Thanx in anticipation.
gshivag
0
gshivag
Asked:
gshivag
1 Solution
 
laeuchliCommented:
I don't know how you got no compiler problems, because this code sure doesn't compile on my system. To call a function pointed to by a pointer try this ptr->Function();
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now