开发者

Output of the Program? [closed]

开发者_StackOverflow中文版 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.
#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


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):

  • http://www.newty.de/fpt/index.html: Function pointers


 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.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜