The following code is working!
function and function pointer are defined
I defined two probe function:
mysin1 and mysin2
both works with myfunc with in first argument
but I dont't understand,
because mysin2 wait for a function_pointer not a function

#include <math.h>
#include <iostream>

using namespace std;

typedef double func(double v);
typedef func *func_pointer;

double myfunc(double x)
{
  return x*x;
}

double mysin1(func infunc, double x)
{
  return sin(infunc(x));
}

double mysin2(func_pointer inpointer, double x)
{
  return sin((*inpointer)(x));
}

int main() 
{
   func newf;
   func_pointer newfp;
 
   cout << mysin1(myfunc,2.) << ", " << sin(4.) << ", " << mysin2(myfunc,2.) << endl;
   return 0;
}

result:
-0.765802, -0.765802,-0.765802

The following code is working!
function and function pointer are defined
I defined two probe function:
mysin1 and mysin2
both works with myfunc with in first argument
but I dont't understand,
because mysin2 wait for a function_pointer not a function

#include <math.h>
#include <iostream>

using namespace std;

typedef double func(double v);
typedef func *func_pointer;

double myfunc(double x)
{
  return x*x;
}

double mysin1(func infunc, double x)
{
  return sin(infunc(x));
}

double mysin2(func_pointer inpointer, double x)
{
  return sin((*inpointer)(x));
}

int main() 
{
   func newf;
   func_pointer newfp;
 
   cout << mysin1(myfunc,2.) << ", " << sin(4.) << ", " << mysin2(myfunc,2.) << endl;
   return 0;
}

result:
-0.765802, -0.765802,-0.765802

ok, well for starters, the reason your call to mysin2 is working is because you are passing in myfunc, it doesn't matter that you haven't passed a strict function pointer, it gets treated as a pointer anyway.
It should also be noted that mysin1 and mysin2 both take function pointers, regardless of how things might look!

So you could pass myfunc to mysin1 or mysin2 in any of three ways:

1. The way you've already used, passing myfunc directly:

..... mysin2(myfunc, 2.) << .....

2. passing using the 'address of' operator (&):

..... mysin2(&myfunc, 2.) << .....

3. assigning your function pointer (newfp) to point to myfunc and then passing newfp to mysin2:

func_pointer newfp = &myfunc; // or newfp=myfunc;
..... mysin2(newfp, 2.) << .....

Although all three methods work, I think the 3rd is the way that you meant to try. So your code should've looked something like this:

#include <math.h>
#include <iostream>

using namespace std;

typedef double func(double v);
typedef func *func_pointer;

double myfunc(double x)
{
  return x*x;
}

double mysin1(func infunc, double x)
{
  return sin(infunc(x));
}

double mysin2(func_pointer inpointer, double x)
{
  return sin((*inpointer)(x));
}

int main() 
{
   //func newf; - Not needed remove this
   func_pointer newfp = &myfunc; // assign your function pointer to point to myfunc
   // note: func_pointer newfp = myfunc; will work too!
 
   // Now pass newfp into mysin2...
   cout << mysin1(myfunc,2.) << ", " << sin(4.) << ", " << mysin2(newfp,2.) << endl;
   return 0;
}

Now this code probably looks a lot more like you were expecting!
But because of the way function pointers work, you could've used any of the three methods outlined previously to pass the function into mysin1 and mysin2.


So to recap, regardless of how things may look, mysin1 and mysin2 actually both take pointers to functions as parameters.
The three ways I've shown of passing the function pointer into the function are equivalent....The result is the same, a function pointer is passed into the function.

I hope this is of some help and hasn't confused you too much!
Cheers for now,
Jas.

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.