function pointers

S

Srinivas

Hi all,
Can anyone describe how the following program works:

#include<stdio.h>

int sum(int ,int);

int main()
{
int (*ptr)(int ,int);
int result;

ptr=sum;

result=test(&************&*******&********&*******&*****&sum);
printf("%d\n",result);

return 0;
}

int test(int (*ptr)(int ,int ))
{

return ptr(2,3);
}

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

srinivas
 
M

Michael Mair

Srinivas said:
Hi all,
Can anyone describe how the following program works:

Yes. In order to give you the opportunity to formulate
your exact question, I will give a slightly different
version of your code and comment on a couple of things
which are there.
#include<stdio.h>

#include <stdio.h>

White space and its consistent use makes reading code easier.
int sum(int ,int);

It does not hurt to give the prototype in the same form
as the definition:
int sum (int a, int b);

This also gives the compiler opportunity to warn you about
a change in parameter names -- if the parameter name
changes, the semantics sometimes change, too.

You forgot a prototype for test().

int main()

int main (void)
for completeness
{
int (*ptr)(int ,int);
int result;

ptr=sum;

ptr is not really used, i.e. unnecessary.

result=test(&************&*******&********&*******&*****&sum);

Now, what's that?
result = test(sum);
or
result = test(ptr);
do just fine
printf("%d\n",result);

return 0;
}

int test(int (*ptr)(int ,int ))
{

return ptr(2,3);

This is fine; in order to emphasize that you are working
with a function pointer, you can also use
return (*ptr)(2, 3);
}

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

Do you want to know something about function pointers?

Cheers
Michael

#include <stdio.h>

int sum (int a, int b);
int test (int (*ptr)(int, int));


int main (void)
{
int result;

result = test(sum);
printf("%d\n", result);

return 0;
}


int test (int (*ptr)(int, int))
{
return (*ptr)(2, 3);
}


int sum (int a, int b)
{
return a + b;
}
 
J

junky_fellow

Michael said:
Yes. In order to give you the opportunity to formulate
your exact question, I will give a slightly different
version of your code and comment on a couple of things
which are there.


#include <stdio.h>

White space and its consistent use makes reading code easier.


It does not hurt to give the prototype in the same form
as the definition:
int sum (int a, int b);

This also gives the compiler opportunity to warn you about
a change in parameter names -- if the parameter name
changes, the semantics sometimes change, too.

You forgot a prototype for test().



int main (void)
for completeness

ptr is not really used, i.e. unnecessary.
<snip>

I unable to understand how the above statement is parsed ?
Can you please explain this ?
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top