tags:

views:

134

answers:

5
#include<stdio.h>

int fun(int, int);
typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
    printf("%d\n", proc(fun, 6, 6));
    return 0;
}
int fun(int a, int b)
{
   return (a==b);
}
int proc(pf p, int a, int b)
{
   return ((*p)(a, b));
}

// direct link of program : http://codepad.org/fBIPiHGT

A: 

1

Function Pointer to "fun" is passed to proc, which evalutes 6==6, returns true, which is converted impliclty to "1".

http://codepad.org/uRbOnkXb

RJFalconer
+4  A: 

The program's output is:

1

Ok, so let's see what's going on there.

#include<stdio.h>

This line just include standard input/output functionality.

int fun(int, int);

This tells the compiler: Ok, we have a function named fun taking two int variables, returning an int.

typedef int (*pf) (int, int);

This installs kinda shortcut for a pointer to a function taking two int variables returning int, so this kind of function pointer can be abbreviated using pf.

int proc(pf, int, int);

Tells the compiler: Ok, we have a function named proc taking a pf variable (which is - like we saw above - a function pointer to a function taking two int variables returning an int), two int variables, returning an int.

int main()
{
    printf("%d\n", proc(fun, 6, 6));
    return 0;
}

The central procedure that's run when the program is executed. It tells the program to print a number (%d) and a newline (\n). The number shall have the value of proc(fun,6,6).

int fun(int a, int b)
{
   return (a==b);
}

Here we have what function fun is supposed to do. This function compares a and b, returns 1 if they're equal, 0 otherwise (just the definition of results of == in C).

int proc(pf p, int a, int b)
{
   return ((*p)(a, b));
}

Here we have what proc actually does: It takes a function pointer (which is - as we saw above - ...) p and two ints. Then it calls the given function (p) applied to the two arguments given (a and b) and returns p's result.

So, if we call proc(fun, 6, 6), proc will call fun(6,6), which evaluates to 1 (since 6==6) and returns this result (1).

So, the output will be just

1

But honestly: Please have a look at some things and try to figure out things yourself before just asking (why is the output this-and-that):

phimuemue
Great effort. Many devs who have skipped assembler have conceptual problems with a function pointer as a variable.
nonnb
+1 -I realise its sunday when i read the "DETAILED" explanation.
Praveen S
+1 for *Please have a look at some things and try to figure out things yourself before just asking* :)
liaK
A: 

It is returning true (AKA 1). Since you are returning an int, the boolean value is being implicitly converted from true to 1. Program is operating correctly. 6 equals 6 which yields true or 1.

0A0D
I know .. answer is 1 .. bt how i didn't get how the function is calling and tycasting process. plz if u know explain .
because you are passing address to the fun function into your proc function and the compiler knows what fun is so it calls it when you say `return ((*p)(a, b));`
0A0D
@Vish: See also http://publications.gbdirect.co.uk/c_book/chapter5/function_pointers.html
0A0D
@0A0D thanks thanks link... really useful.. dude . can u provide me useful websites where i can solve . C programs,problems to gain more knowledge .. in C programming
@Vish: You're on one now :) Recommend you go to the book store or Amazon.com and purchase some programming books and teach yourself.. take some college courses, etc
0A0D
+2  A: 

Type pf is a function type. In main(), function fun is passed to function proc, so:

proc(fun, 6, 6) = fun(6, 6) = 6==6 = 1

Maurice Perry
+2  A: 
 printf("%d\n", proc(fun, 6, 6));

Outputs the result of proc as an int.

 return ((*p)(a, b));

Returns the result of running p(a,b), ergo fun(6,6)

 return (a==b);

6 == 6 returns true, casted to an int equals 1.

All because of:

int(true) == 1

As for the line:

return ((*p)(a, b));

... is the same as:

return (*p)(a, b);

What we are doing here is dereferencing the function pointer p so a call can be made with the passed parameters. The (*p) is the dereferenced pointer, and the (a, b) are the parameters. p points to fun, so *p is fun.

Kornel Kisielewicz
typedef int (*pf) (int, int);wt happens in this process ?
@Vishwanath, I expanded my answer.
Kornel Kisielewicz
It defines "pf" as a pointer to a function returning int, that takes two ints as parameters.
RJFalconer
@Kornel thanks friend .. nw i got it .. really thanks