function pointers as function parameters

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

    #1

    function pointers as function parameters

    I am experimenting with function pointers. Unfortunately, my C book has
    nothing on function pointers as function parameters. I want to pass a
    pointer to ff() to f() with the result that f() prints the return value
    of ff(). The code below seems to work, but I would appreciate your
    comments. Have I got it right? Does the function name "decay" to a pointer?


    #include <stdio.h>
    /* declares a function which takes an argument
    that is a pointer to a function returning an int */
    void f(int (*fptr)());
    /* function returning an int */
    int ff(void);

    int main(void)
    {
    f(ff); /* pass the address of ff to f */

    return 0;
    }

    void f(int (*fptr)())
    {
    int a;
    a = (*fptr)(); /* deref the func pointer */
    printf("%d\n", a);
    return;
    }

    int ff(void)
    {
    return 2345;
    }
  • Eric Sosman

    #2
    Re: function pointers as function parameters



    Marlene Stebbins wrote:[color=blue]
    > I am experimenting with function pointers. Unfortunately, my C book has
    > nothing on function pointers as function parameters. I want to pass a
    > pointer to ff() to f() with the result that f() prints the return value
    > of ff(). The code below seems to work, but I would appreciate your
    > comments. Have I got it right? Does the function name "decay" to a pointer?[/color]

    It might be simpler to say that the function name "is"
    a pointer to the function, since there is no context in
    which it does anything other than "decay."

    Your code is correct (as far as I can see), but there
    are a few opportunities for stylistic improvement:
    [color=blue]
    > #include <stdio.h>
    > /* declares a function which takes an argument
    > that is a pointer to a function returning an int */
    > void f(int (*fptr)());[/color]

    You know more about the pointed-to function than the
    type of its returned value: specifically, you know that
    it takes no arguments. (How do you know this? Because
    you supply no arguments when you call it.) On the general
    principle that it's best to tell the compiler everything
    you know, write the declaration as

    void f(int (*fptr)(void));

    This way, the compiler will protest if you inadvertently
    try to call the pointed-to function with arguments. If
    you do in fact want to pass arguments, say so:

    void f2(int (*fptr)(double, int, const char*));
    [color=blue]
    > /* function returning an int */
    > int ff(void);
    >
    > int main(void)
    > {
    > f(ff); /* pass the address of ff to f */[/color]

    Here's where ff "decays."
    [color=blue]
    > return 0;
    > }
    >
    > void f(int (*fptr)())[/color]

    If you've provided a prototype as suggested above,
    you should also do so here.
    [color=blue]
    > {
    > int a;
    > a = (*fptr)(); /* deref the func pointer */[/color]

    This can also be written `a = fptr();' without the
    parentheses and the asterisk. Some people prefer to write
    the call as you've done it, saying that it draws attention
    to the fact that a function pointer variable (rather than
    a function identifier) is being used. I personally don't
    buy that argument, noting that

    (*printf)("Hell o, world!\n");

    is equally legitimate and (IMHO) equally silly. However,
    de gustibus non disputandum est (Latin for "There's just
    no arguing with Gus").
    [color=blue]
    > printf("%d\n", a);
    > return;
    > }
    >
    > int ff(void)
    > {
    > return 2345;
    > }[/color]

    --
    Eric.Sosman@sun .com

    Comment

    • pete

      #3
      Re: function pointers as function parameters

      Eric Sosman wrote:[color=blue]
      >
      > Marlene Stebbins wrote:[color=green]
      > > I am experimenting with function pointers.
      > > Unfortunately, my C book has
      > > nothing on function pointers as function parameters.
      > > I want to pass a
      > > pointer to ff() to f() with the result that f()
      > > prints the return value
      > > of ff(). The code below seems to work, but I would appreciate your
      > > comments. Have I got it right?
      > > Does the function name "decay" to a pointer?[/color]
      >
      > It might be simpler to say that the function name "is"
      > a pointer to the function, since there is no context in
      > which it does anything other than "decay."[/color]

      The function name remains an expression of a function type
      when it is an operand of the address operator.
      You can force a non macro invocation of a standard library
      function by calling it like
      (&putchar)('\n' );
      which wouldn't make sense if putchar were a pointer.
      If a function name were a pointer expression,
      then it would be a valid operand of the sizeof operator.
      For those two reasons,
      I would not say that a function name is a pointer.

      --
      pete

      Comment

      • Emmanuel Delahaye

        #4
        Re: function pointers as function parameters

        Marlene Stebbins wrote on 02/05/05 :[color=blue]
        > #include <stdio.h>
        > /* declares a function which takes an argument
        > that is a pointer to a function returning an int */
        > void f(int (*fptr)());
        > /* function returning an int */
        > int ff(void);
        >
        > int main(void)
        > {
        > f(ff); /* pass the address of ff to f */
        >
        > return 0;
        > }
        >
        > void f(int (*fptr)())
        > {
        > int a;
        > a = (*fptr)(); /* deref the func pointer */
        > printf("%d\n", a);
        > return;
        > }
        >
        > int ff(void)
        > {
        > return 2345;
        > }[/color]

        You've got it (jist add 'void' into the () and it's all fine). Now, try
        the Simple Way:

        #include <stdio.h>

        typedef int F (void);

        static int ff (void)
        {
        return 2345;
        }

        static void f (F * pf)
        {
        /* deref the func pointer */
        int a = pf ();

        printf ("%d\n", a);
        return;
        }

        int main (void)
        {
        /* pass the address of ff to f */
        f (ff);

        return 0;
        }

        --
        Emmanuel
        The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
        The C-library: http://www.dinkumware.com/refxc.html

        "Clearly your code does not meet the original spec."
        "You are sentenced to 30 lashes with a wet noodle."
        -- Jerry Coffin in a.l.c.c++

        Comment

        • Keith Thompson

          #5
          Re: function pointers as function parameters

          Eric Sosman <eric.sosman@su n.com> writes:[color=blue]
          > Marlene Stebbins wrote:[color=green]
          >> I am experimenting with function pointers. Unfortunately, my C book has
          >> nothing on function pointers as function parameters. I want to pass a
          >> pointer to ff() to f() with the result that f() prints the return value
          >> of ff(). The code below seems to work, but I would appreciate your
          >> comments. Have I got it right? Does the function name "decay" to a pointer?[/color]
          >
          > It might be simpler to say that the function name "is"
          > a pointer to the function, since there is no context in
          > which it does anything other than "decay."[/color]

          Almost. A function name decays (is implicitly converted) to a pointer
          in most contexts. The exceptions are when it's the operand of a unary "&"
          (&ff yields a pointer-to-function, not a pointer-to-pointer-to-function),
          and when it's the operand of sizeof (sizeof ff is illegal; if it
          decayed in that context it would yield the size of a function
          pointer).

          [snip][color=blue]
          > This can also be written `a = fptr();' without the
          > parentheses and the asterisk. Some people prefer to write
          > the call as you've done it, saying that it draws attention
          > to the fact that a function pointer variable (rather than
          > a function identifier) is being used. I personally don't
          > buy that argument, noting that
          >
          > (*printf)("Hell o, world!\n");
          >
          > is equally legitimate and (IMHO) equally silly. However,
          > de gustibus non disputandum est (Latin for "There's just
          > no arguing with Gus").[/color]

          Note that
          (*printf)("Hell o, world!\n");
          isn't exactly equivalent to
          printf("Hello, world!\n");
          since the latter could invoke a macro. That's a special rule for
          functions declared in standard headers. But if you want to avoid the
          macro and call the actual function, you can just use parentheses:

          (printf)("Hello , world!\n");

          Hmm. Actually, since printf() takes a variable number of arguments,
          implementing it as a macro is not possible in C90; I'm not sure
          whether it's possible in C99. But I digress.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Lawrence Kirby

            #6
            Re: function pointers as function parameters

            On Mon, 02 May 2005 18:59:12 +0000, pete wrote:
            [color=blue]
            > Eric Sosman wrote:[color=green]
            >>
            >> Marlene Stebbins wrote:[color=darkred]
            >> > I am experimenting with function pointers.
            >> > Unfortunately, my C book has
            >> > nothing on function pointers as function parameters.
            >> > I want to pass a
            >> > pointer to ff() to f() with the result that f()
            >> > prints the return value
            >> > of ff(). The code below seems to work, but I would appreciate your
            >> > comments. Have I got it right?
            >> > Does the function name "decay" to a pointer?[/color]
            >>
            >> It might be simpler to say that the function name "is"
            >> a pointer to the function, since there is no context in
            >> which it does anything other than "decay."[/color]
            >
            > The function name remains an expression of a function type
            > when it is an operand of the address operator.
            > You can force a non macro invocation of a standard library
            > function by calling it like
            > (&putchar)('\n' );[/color]

            That's true although in practice it tends to be written as

            (putchar)('\n') ;
            [color=blue]
            > which wouldn't make sense if putchar were a pointer.[/color]

            But, yes, your form makes this point.
            [color=blue]
            > If a function name were a pointer expression,
            > then it would be a valid operand of the sizeof operator.
            > For those two reasons,
            > I would not say that a function name is a pointer.[/color]

            It is a very similar argument to that for array names. Array names have
            array type but in many cases an array "value" is given as a pointer to the
            array's first element, so in those cases it appears to be a pointer. But
            it isn't and as with functions &array and sizeof array show its true
            nature.

            Lawrence

            Comment

            • pete

              #7
              Re: function pointers as function parameters

              Lawrence Kirby wrote:[color=blue]
              >
              > On Mon, 02 May 2005 18:59:12 +0000, pete wrote:
              >[color=green]
              > > Eric Sosman wrote:[/color][/color]
              [color=blue][color=green][color=darkred]
              > >> It might be simpler to say that the function name "is"
              > >> a pointer to the function, since there is no context in
              > >> which it does anything other than "decay."[/color]
              > >
              > > The function name remains an expression of a function type
              > > when it is an operand of the address operator.
              > > You can force a non macro invocation of a standard library
              > > function by calling it like
              > > (&putchar)('\n' );[/color]
              >
              > That's true although in practice it tends to be written as
              >
              > (putchar)('\n') ;
              >[color=green]
              > > which wouldn't make sense if putchar were a pointer.[/color]
              >
              > But, yes, your form makes this point.
              >[color=green]
              > > If a function name were a pointer expression,
              > > then it would be a valid operand of the sizeof operator.
              > > For those two reasons,
              > > I would not say that a function name is a pointer.[/color]
              >
              > It is a very similar argument to that for array names.
              > Array names have array type but in many cases an array "value"
              > is given as a pointer to the array's first element,
              > so in those cases it appears to be a pointer.
              > But it isn't and as with functions &array
              > and sizeof array show its true nature.[/color]

              I would agree with Eric Sosman's statement to the extent
              that the only use of a value of an expression of a function type,
              is that a pointer can be derived from it.

              What constitutes the value of an array type, is more complicated.
              The value of an array type (even an incomplete array type)
              can be converted to a pointer.
              The value of an array type can initialize an array.
              char array[] = "";
              The value of an array type structure member,
              is a byte by byte copy when the structure
              is an operand of the assignment operator.

              --
              pete

              Comment

              • Michael Wojcik

                #8
                Re: function pointers as function parameters


                In article <lnsm158jrc.fsf @nuthaus.mib.or g>, Keith Thompson <kst-u@mib.org> writes:[color=blue]
                > Eric Sosman <eric.sosman@su n.com> writes:[color=green]
                > >
                > > It might be simpler to say that the function name "is"
                > > a pointer to the function, since there is no context in
                > > which it does anything other than "decay."[/color]
                >
                > Almost. A function name decays (is implicitly converted) to a pointer
                > in most contexts. The exceptions are when it's the operand of a unary "&"
                > (&ff yields a pointer-to-function, not a pointer-to-pointer-to-function),
                > and when it's the operand of sizeof (sizeof ff is illegal; if it
                > decayed in that context it would yield the size of a function
                > pointer).[/color]

                I was mulling over the hypothesis that the & and * operators, when
                applied to function names, simply did nothing, but your last example
                made me think of a possible counterexample. Is the following a
                strictly conforming expression?

                sizeof &main;

                Or, if you prefer the full-program version:

                #include <stdio.h>
                int main(void) {
                printf("%lu\n", (unsigned long) sizeof &main);
                return 0;
                }

                If it is, aside from that, are there any other cases where the & and
                * operators have any effect when applied to a function name?

                --
                Michael Wojcik michael.wojcik@ microfocus.com

                "We are facing a dire shortage of clowns," said Erickson, also known as
                Jingles.

                Comment

                • Eric Sosman

                  #9
                  Re: function pointers as function parameters



                  pete wrote:[color=blue]
                  > Lawrence Kirby wrote:
                  >[color=green]
                  >>On Mon, 02 May 2005 18:59:12 +0000, pete wrote:
                  >>
                  >>[color=darkred]
                  >>>Eric Sosman wrote:[/color][/color]
                  >
                  >[color=green][color=darkred]
                  >>>> It might be simpler to say that the function name "is"
                  >>>>a pointer to the function, since there is no context in
                  >>>>which it does anything other than "decay."
                  >>>
                  >>>The function name remains an expression of a function type
                  >>>when it is an operand of the address operator.
                  >>>You can force a non macro invocation of a standard library
                  >>>function by calling it like
                  >>> (&putchar)('\n' );[/color]
                  >>
                  >>That's true although in practice it tends to be written as
                  >>
                  >> (putchar)('\n') ;
                  >>
                  >>[color=darkred]
                  >>>which wouldn't make sense if putchar were a pointer.[/color]
                  >>
                  >>But, yes, your form makes this point.
                  >>
                  >>[color=darkred]
                  >>>If a function name were a pointer expression,
                  >>>then it would be a valid operand of the sizeof operator.
                  >>>For those two reasons,
                  >>>I would not say that a function name is a pointer.[/color]
                  >>
                  >>It is a very similar argument to that for array names.
                  >>Array names have array type but in many cases an array "value"
                  >>is given as a pointer to the array's first element,
                  >>so in those cases it appears to be a pointer.
                  >>But it isn't and as with functions &array
                  >>and sizeof array show its true nature.[/color]
                  >
                  >
                  > I would agree with Eric Sosman's statement to the extent
                  > that the only use of a value of an expression of a function type,
                  > is that a pointer can be derived from it.[/color]

                  Although it's flattering to learn that someone agrees with
                  me, it is also a bit embarrassing -- because I have come to
                  *dis*agree with myself ;-)

                  I said there was no context in which a function identifier
                  doesn't "decay" to a pointer to the function, but Keith
                  Thompson has convinced me I was wrong. Specifically:

                  - In the context `&func', the `func' part does not behave
                  like a pointer-to-function. If it did, the entire
                  expression would be pointer-to-pointer-to-function --
                  but, strangely enough, both `func' and `&func' (and
                  even `&&&&&&&func' ) are pointer-to-function.

                  - In the context `sizeof func', the `func' part does not
                  behave like a pointer-to-function. If it did, the
                  expression would evaluate to the size of a function
                  pointer, but in actuality the expression produces a
                  diagnostic.

                  The second point is debatable: since the context is not
                  valid C at all, its odd behavior doesn't matter. One might
                  equally well make arguments about the array-indexing operator
                  in the context `[4]"Hello"'; interesting things might be said,
                  but they'd have little to do with C. But even if one rejects
                  the second point, the first seems unassailable: Keith is right
                  and I was wrong, R-O-N-G, wrong.

                  --
                  Eric.Sosman@sun .com

                  Comment

                  • Jonathan Bartlett

                    #10
                    Re: function pointers as function parameters

                    Your code is good. Let me also point you to an article I wrote recently
                    involving the subject:



                    Some of the code in there is non-standard C, but works for most
                    platforms most people deal with.

                    Jon
                    ----
                    Learn to program using Linux assembly language

                    Comment

                    • Lawrence Kirby

                      #11
                      Re: function pointers as function parameters

                      On Tue, 03 May 2005 10:57:05 -0400, Eric Sosman wrote:

                      ....
                      [color=blue]
                      > I said there was no context in which a function identifier
                      > doesn't "decay" to a pointer to the function, but Keith
                      > Thompson has convinced me I was wrong. Specifically:
                      >
                      > - In the context `&func', the `func' part does not behave
                      > like a pointer-to-function. If it did, the entire
                      > expression would be pointer-to-pointer-to-function --
                      > but, strangely enough, both `func' and `&func' (and
                      > even `&&&&&&&func' ) are pointer-to-function.[/color]

                      Using the && operator like this is a syntax error. But even
                      (& &func) is invalid because unary & requires an lvalue or function
                      designator as its operand, but it doesn't produce either as its result.

                      Lawrence

                      Comment

                      • Keith Thompson

                        #12
                        Re: function pointers as function parameters

                        Eric Sosman <eric.sosman@su n.com> writes:
                        [...][color=blue]
                        > Although it's flattering to learn that someone agrees with
                        > me, it is also a bit embarrassing -- because I have come to
                        > *dis*agree with myself ;-)
                        >
                        > I said there was no context in which a function identifier
                        > doesn't "decay" to a pointer to the function, but Keith
                        > Thompson has convinced me I was wrong. Specifically:
                        >
                        > - In the context `&func', the `func' part does not behave
                        > like a pointer-to-function. If it did, the entire
                        > expression would be pointer-to-pointer-to-function --
                        > but, strangely enough, both `func' and `&func' (and
                        > even `&&&&&&&func' ) are pointer-to-function.[/color]

                        As Lawrence Kirby pointed out, this is illegal because the "maximal
                        munch" rule turns this into three "&&" operators followed by an "&"
                        operator; once you fix that, it's still illegal because &func isn't a
                        lvalue. But you can have &func, func, *func, **func, *****func, and
                        *&*&*&*&func.
                        [color=blue]
                        > - In the context `sizeof func', the `func' part does not
                        > behave like a pointer-to-function. If it did, then
                        > expression would evaluate to the size of a function
                        > pointer, but in actuality the expression produces a
                        > diagnostic.
                        >
                        > The second point is debatable: since the context is not
                        > valid C at all, its odd behavior doesn't matter. One might
                        > equally well make arguments about the array-indexing operator
                        > in the context `[4]"Hello"'; interesting things might be said,
                        > but they'd have little to do with C. But even if one rejects
                        > the second point, the first seems unassailable: Keith is right
                        > and I was wrong, R-O-N-G, wrong.[/color]

                        I hate to force you to disagree with yourself even further, but here's
                        what the standard says:

                        C99 6.3.2.1p4:

                        A function designator is an expression that has function
                        type. Except when it is the operand of the sizeof operator(54) or
                        the unary & operator, a function designator with type "function
                        returning type" is converted to an expression that has type
                        "pointer to function returning type".

                        And footnote (54):

                        Because this conversion does not occur, the operand of the sizeof
                        operator remains a function designator and violates the constraint
                        in 6.5.3.4.

                        6.5.3.4p1 says:

                        Constraints

                        The sizeof operator shall not be applied to an expression that has
                        function type or an incomplete type, to the parenthesized name of
                        such a type, or to an expression that designates a bit-field
                        member.

                        It seems a little convoluted to make an exception to the rule for the
                        operand of a sizeof operator, only to make it a constraint violation
                        in a different section of the standard, but I think this is actually
                        the most straightforward way to do it. The rule is similar to the
                        rule for arrays, except that it makes sense to apply sizeof to an
                        array value.

                        [4]"Hello", on the other hand, is a syntax error, not a constraint
                        violation. (And as you know, 4["Hello"] is perfectly legal.)

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • pete

                          #13
                          Re: function pointers as function parameters

                          Michael Wojcik wrote:
                          [color=blue]
                          > sizeof &main;[/color]

                          That's the size of a pointer.
                          [color=blue]
                          > If it is, aside from that, are there any other cases where the & and
                          > * operators have any effect when applied to a function name?[/color]

                          The * operator always converts the function type expression
                          to a pointer, prior to operating on it.

                          These are all valid function calls:

                          (putchar)('\n') ;
                          (&putchar)('\n' );
                          (*putchar)('\n' );

                          In the first one, the function type expression,
                          is converted to a pointer, by the function call operator.

                          The second one has no conversions.

                          In the third one, the * operator converts the function type
                          expression to a pointer, then operates on it,
                          the result is an expression of function type
                          which is converted to a pointer by the function call operator.

                          --
                          pete

                          Comment

                          • pete

                            #14
                            Re: function pointers as function parameters

                            Eric Sosman wrote:[color=blue]
                            >
                            > pete wrote:[color=green]
                            > > Lawrence Kirby wrote:
                            > >[color=darkred]
                            > >>On Mon, 02 May 2005 18:59:12 +0000, pete wrote:
                            > >>
                            > >>
                            > >>>Eric Sosman wrote:[/color]
                            > >
                            > >[color=darkred]
                            > >>>> It might be simpler to say that the function name "is"
                            > >>>>a pointer to the function, since there is no context in
                            > >>>>which it does anything other than "decay."
                            > >>>
                            > >>>The function name remains an expression of a function type
                            > >>>when it is an operand of the address operator.
                            > >>>You can force a non macro invocation of a standard library
                            > >>>function by calling it like
                            > >>> (&putchar)('\n' );
                            > >>
                            > >>That's true although in practice it tends to be written as
                            > >>
                            > >> (putchar)('\n') ;
                            > >>
                            > >>
                            > >>>which wouldn't make sense if putchar were a pointer.
                            > >>
                            > >>But, yes, your form makes this point.
                            > >>
                            > >>
                            > >>>If a function name were a pointer expression,
                            > >>>then it would be a valid operand of the sizeof operator.
                            > >>>For those two reasons,
                            > >>>I would not say that a function name is a pointer.
                            > >>
                            > >>It is a very similar argument to that for array names.
                            > >>Array names have array type but in many cases an array "value"
                            > >>is given as a pointer to the array's first element,
                            > >>so in those cases it appears to be a pointer.
                            > >>But it isn't and as with functions &array
                            > >>and sizeof array show its true nature.[/color]
                            > >
                            > >
                            > > I would agree with Eric Sosman's statement to the extent
                            > > that the only use of a value of an expression of a function type,
                            > > is that a pointer can be derived from it.[/color]
                            >
                            > Although it's flattering to learn that someone agrees with
                            > me, it is also a bit embarrassing -- because I have come to
                            > *dis*agree with myself ;-)
                            >
                            > I said there was no context in which a function identifier
                            > doesn't "decay" to a pointer to the function, but Keith
                            > Thompson has convinced me I was wrong. Specifically:
                            >
                            > - In the context `&func', the `func' part does not behave
                            > like a pointer-to-function. If it did, the entire
                            > expression would be pointer-to-pointer-to-function --
                            > but, strangely enough, both `func' and `&func' (and
                            > even `&&&&&&&func' ) are pointer-to-function.
                            >
                            > - In the context `sizeof func', the `func' part does not
                            > behave like a pointer-to-function. If it did, the
                            > expression would evaluate to the size of a function
                            > pointer, but in actuality the expression produces a
                            > diagnostic.
                            >
                            > The second point is debatable: since the context is not
                            > valid C at all, its odd behavior doesn't matter.[/color]

                            The fact that the function name is not a pointer, is the
                            reason why the function name is not a valid operand of sizeof.
                            Function pointers are object types and valid operands of sizeof.
                            So, if the point is whether or not function names are pointers,
                            then the sizeof issue is relevant.
                            [color=blue]
                            > One might
                            > equally well make arguments about the array-indexing operator
                            > in the context `[4]"Hello"'; interesting things might be said,
                            > but they'd have little to do with C.[/color]

                            The array indexing operator converts the array name to a pointer,
                            just as the * operator does with both array names and function names.

                            --
                            pete

                            Comment

                            • Keith Thompson

                              #15
                              Re: function pointers as function parameters

                              pete <pfiland@mindsp ring.com> writes:[color=blue]
                              > Michael Wojcik wrote:
                              >[color=green]
                              >> sizeof &main;[/color]
                              >
                              > That's the size of a pointer.
                              >[color=green]
                              >> If it is, aside from that, are there any other cases where the & and
                              >> * operators have any effect when applied to a function name?[/color]
                              >
                              > The * operator always converts the function type expression
                              > to a pointer, prior to operating on it.[/color]

                              No, the * operator doesn't do any conversions. The conversion occurs
                              before the * operator is invoked. Perhaps this is just a matter of
                              terminology, but in my opinion saying that the operator converts its
                              operand is misleading. The * operator expects a pointer operand; it
                              gets one because the operand is already a pointer.

                              Given
                              int n;
                              the expression *n is illegal. If the * operator had the ability to
                              convert its operand to a pointer type, *n would be equivalent to n.
                              You could argue, I suppose, that the * operator converts its operand
                              only in certain circumstances, but I don't see any support for this
                              idea in the standard.
                              [color=blue]
                              > These are all valid function calls:
                              >
                              > (putchar)('\n') ;
                              > (&putchar)('\n' );
                              > (*putchar)('\n' );
                              >
                              > In the first one, the function type expression,
                              > is converted to a pointer, by the function call operator.[/color]

                              The expression putchar is of function type. Since it's not the
                              operand of a sizeof or "&" operator, it's implicitly converted to a
                              pointer-to-function. The parenthesized expression is a no-op, used
                              only for syntactic resolution. This pointer-to-function *then*
                              becomes an operand of the function call operator; since the function
                              call operator expects a pointer-to-function, no further conversion is
                              necessary.
                              [color=blue]
                              > The second one has no conversions.[/color]

                              Agreed.
                              [color=blue]
                              > In the third one, the * operator converts the function type
                              > expression to a pointer, then operates on it,
                              > the result is an expression of function type
                              > which is converted to a pointer by the function call operator.[/color]

                              The expression putchar is of function type. Since it's not the
                              operand of a sizeof or "&" operator, it's implicitly converted to a
                              pointer-to-function. The "*" operator expects, and gets, a pointer as
                              its operand; it yields an lvalue designating the thing the pointer
                              points to. In this case, the result is an lvalue designating the
                              putchar function. This is, again, an expression of function type.
                              Since this is also not the operand of a sizeof or "&" operator, it's
                              again implicitly converted to a pointer-to-function type. The
                              parentheses again have no semantic effect. The function call operator
                              expects, and gets, a pointer-to-function as its operand.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                              We must do something. This is something. Therefore, we must do this.

                              Comment

                              Working...