• C

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
gshivagAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.