Function definition with pointers

Hi... I am finding the following part a bit difficult as I find hard working with pointers... Somebody please help me out...

Last edited on
1
2
3
4
5
6
typedef int(*funtion_pointer)(const std::string&);

int sum( function_pointer arr[], int len ){
   int s = (*arr[0])("123");
   //and so on...
}


Though why do you need multiple functions the do the same thing?
Though why do you need multiple functions the do the same thing?

I'm sure the OP doesn't, but there's no need to clutter the question with all the different implementations.

After seeing one example of how it's done, the OP can easily replace the implementations to do something more interesting.
Last edited on
Hi,

Thanks for the reply... but I am still not clear since I am very bad at understanding the function pointers at first stage and then the question itself isn't very clear to me...

Could someone please help me here so that I can solve the question or help me in solving the question.
Basically, the (IMO) clearest way to define function ptrs is with a typedef. The syntax looks like this, as hamsterman showed in his post:
typedef RETURN_TYPE (*TYPEDEF_NAME)(PARAMETER_LIST);
Now to call a function pointer, you simply dereference the name of the pointer and call it like a function.
1
2
TYPEDEF_NAME func_ptr;
(*func_ptr)();

Consider this simple situation, where you have a function that takes no parameters:
1
2
3
4
5
int func(); // function
typedef int (*ptr_type)(); // function pointer type
ptr_type ptr = &func; // create a pointer and point it to the original function
func(); // This is the same as
(*ptr)(); // this. 
What is the difference between these? are both valid?
1
2
ptr_type ptr = &func;
(*prt)();
1
2
ptr_type ptr = func;
ptr();

No, a function pointer is a pointer, so ptr() or ptr = func is technically not valid. (Although some compilers support it anyway, since it is "obvious" I guess)
Topic archived. No new replies allowed.