Function Pointer

M

Michael

Hi,

My understanding of
int op(int x, int y, int (*func)(int,int)).

when n = op (20, m, minus); minus is a function pointer, which
matches parameter.
when m = op (7, 5, addition);there is a sort of conversion, is that?
addition is function not a point function. How to undestand here.

Thanks in advance,
Michael

#include <iostream>
using namespace std;

int add(int a, int b)
{ return (a+b); }

int sub(int a, int b)
{ return (a-b); }

int (*minus)(int,int) = subtraction;

int op(int x, int y, int (*func)(int,int))
{
int g;
g = (*func)(x,y);
return (g);
}

int main ()

{
int m,n;
m = op (77, 15, addition);
n = op (20, m, minus);
cout <<n;
return 0;
}
 
V

Victor Bazarov

Michael said:
My understanding of
int op(int x, int y, int (*func)(int,int)).

when n = op (20, m, minus); minus is a function pointer, which
matches parameter.
when m = op (7, 5, addition);there is a sort of conversion, is
that? addition is function not a point function.

No, it isn't. There is no "addition" in your program.
How to undestand
here.

Thanks in advance,
Michael

#include <iostream>
using namespace std;

int add(int a, int b)
{ return (a+b); }

int sub(int a, int b)
{ return (a-b); }

int (*minus)(int,int) = subtraction;

What's "subtraction"? Did you mean

int (*minus)(int,int) = sub;

?
int op(int x, int y, int (*func)(int,int))
{
int g;
g = (*func)(x,y);
return (g);
}

int main ()

{
int m,n;
m = op (77, 15, addition);

Again, I see no "addition" in your program. Did you mean

m = op (77, 15, add);

??
n = op (20, m, minus);

Or you could write

n = op (20, m, sub);
cout <<n;
return 0;
}

Generally a function when used by name degrades to a pointer to it.
Similar to an array when used in an expression by name degrades to the
pointer to the first element. So, yes, when you write 'add', it becomes
a pointer to a function of certain type.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top