function pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Srinivas

    #1

    function pointers

    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",r esult);

    return 0;
    }

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

    return ptr(2,3);
    }

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

    srinivas

  • Michael Mair

    #2
    Re: function pointers

    Srinivas wrote:[color=blue]
    > Hi all,
    > Can anyone describe how the following program works:[/color]

    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.
    [color=blue]
    > #include<stdio. h>[/color]

    #include <stdio.h>

    White space and its consistent use makes reading code easier.
    [color=blue]
    > int sum(int ,int);[/color]

    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().

    [color=blue]
    > int main()[/color]

    int main (void)
    for completeness[color=blue]
    > {
    > int (*ptr)(int ,int);
    > int result;
    >
    > ptr=sum;[/color]

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

    [color=blue]
    > result=test(&** **********&**** ***&********&** *****&*****&sum );[/color]

    Now, what's that?
    result = test(sum);
    or
    result = test(ptr);
    do just fine
    [color=blue]
    > printf("%d\n",r esult);
    >
    > return 0;
    > }
    >
    > int test(int (*ptr)(int ,int ))
    > {
    >
    > return ptr(2,3);[/color]

    This is fine; in order to emphasize that you are working
    with a function pointer, you can also use
    return (*ptr)(2, 3);
    [color=blue]
    > }
    >
    > int sum(int a,int b)
    > {
    > return a+b;
    > }[/color]

    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;
    }

    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    • junky_fellow@yahoo.co.in

      #3
      Re: function pointers


      Michael Mair wrote:[color=blue]
      > Srinivas wrote:[color=green]
      > > Hi all,
      > > Can anyone describe how the following program works:[/color]
      >
      > 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.
      >[color=green]
      > > #include<stdio. h>[/color]
      >
      > #include <stdio.h>
      >
      > White space and its consistent use makes reading code easier.
      >[color=green]
      > > int sum(int ,int);[/color]
      >
      > 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().
      >
      >[color=green]
      > > int main()[/color]
      >
      > int main (void)
      > for completeness[color=green]
      > > {
      > > int (*ptr)(int ,int);
      > > int result;
      > >
      > > ptr=sum;[/color]
      >
      > ptr is not really used, i.e. unnecessary.
      >
      >[color=green]
      > > result=test(&** **********&**** ***&********&** *****&*****&sum );[/color]
      >[/color]
      <snip>

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

      Comment

      Working...