[newbie] de-reference function

Discussion in 'C++' started by Tony Winslow, Sep 7, 2008.

  1. Tony Winslow

    Tony Winslow Guest

    Hi,

    I'm wondering why the following code works just fine:

    #include <stdio.h>

    int main()
    {
    (******printf)("Hello!\n");

    return 0;
    }

    best regards,
    Tony Winslow
     
    Tony Winslow, Sep 7, 2008
    #1
    1. Advertising

  2. Tony Winslow

    red floyd Guest

    Tony Winslow wrote:
    > Hi,
    >
    > I'm wondering why the following code works just fine:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > (******printf)("Hello!\n");
    >
    > return 0;
    > }


    I'm not sure. Gcc took it with -ansi -Wall -pedantic
    Comeau took it in strict mode.

    That looks really wierd to me.
     
    red floyd, Sep 7, 2008
    #2
    1. Advertising

  3. Tony Winslow

    red floyd Guest

    Victor Bazarov wrote:
    > red floyd wrote:
    >> Tony Winslow wrote:
    >>> Hi,
    >>>
    >>> I'm wondering why the following code works just fine:
    >>>
    >>> #include <stdio.h>
    >>>
    >>> int main()
    >>> {
    >>> (******printf)("Hello!\n");
    >>>
    >>> return 0;
    >>> }

    >>
    >> I'm not sure. Gcc took it with -ansi -Wall -pedantic
    >> Comeau took it in strict mode.
    >>
    >> That looks really wierd to me.

    >
    > What's so weird? Since the * associate from right to left, you get
    >
    > *(*(*(*(*(*(printf))))))
    >
    > The 'printf' expression yields a pointer to function, right? Dereference
    > it, and you will get... a an lvalue of the function type, right? So,
    > the first expression
    >
    > *(printf)
    >
    > Gives you an lvalue of function type int(char const*,...). There is no
    > doubt about that, is there? Now, the compiler sees the next dereference
    > operator. What will it try to do? The dereference operator needs a
    > pointer as its argument, right? Now, can an lvalue of function type be
    > converted to some pointer?
    >
    > According to 4.3, if an lvalue of function type T can be converted to an
    > rvalue of type pointer-to-T, the result is a pointer to function. So,
    > the expression
    >
    > (*printf)
    >
    > is converted to a pointer to function. The next dereference yields
    > another lvalue, and so on.
    >


    I figured it was something like that, but it's still pretty weird looking.
     
    red floyd, Sep 7, 2008
    #3
  4. Tony Winslow

    Guest

    Re: de-reference function

    On Sep 7, 11:15 pm, Tony Winslow <> wrote:
    > Hi,
    >
    > I'm wondering why the following code works just fine:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    >         (******printf)("Hello!\n");
    >
    >         return 0;
    >
    > }
    >
    > best regards,
    > Tony Winslow


    That's so cool... I'm going to use it to rate how important my
    function calls are now... 5 stars is really mega-important... 1 is
    kind-of "who cares"...

    Cheers,

    Tony
     
    , Sep 8, 2008
    #4
  5. Tony Winslow

    Tony Winslow Guest

    Victor Bazarov wrote:
    > red floyd wrote:
    >> Tony Winslow wrote:
    >>> Hi,
    >>>
    >>> I'm wondering why the following code works just fine:
    >>>
    >>> #include <stdio.h>
    >>>
    >>> int main()
    >>> {
    >>> (******printf)("Hello!\n");
    >>>
    >>> return 0;
    >>> }

    >>
    >> I'm not sure. Gcc took it with -ansi -Wall -pedantic
    >> Comeau took it in strict mode.
    >>
    >> That looks really wierd to me.

    >
    > What's so weird? Since the * associate from right to left, you get
    >
    > *(*(*(*(*(*(printf))))))
    >
    > The 'printf' expression yields a pointer to function, right? Dereference
    > it, and you will get... a an lvalue of the function type, right? So,
    > the first expression
    >
    > *(printf)
    >
    > Gives you an lvalue of function type int(char const*,...). There is no
    > doubt about that, is there? Now, the compiler sees the next dereference
    > operator. What will it try to do? The dereference operator needs a
    > pointer as its argument, right? Now, can an lvalue of function type be
    > converted to some pointer?
    >
    > According to 4.3, if an lvalue of function type T can be converted to an
    > rvalue of type pointer-to-T, the result is a pointer to function. So,
    > the expression
    >
    > (*printf)
    >
    > is converted to a pointer to function. The next dereference yields
    > another lvalue, and so on.
    >
    > V


    Wow, I understand it now.
    Thanks for your analysis!

    Best regards,
    Tony Winslow
     
    Tony Winslow, Sep 8, 2008
    #5
  6. Re: de-reference function

    wrote:
    >> I'm wondering why the following code works just fine:
    >>
    >> #include <stdio.h>
    >>
    >> int main()
    >> {
    >> (******printf)("Hello!\n");
    >>
    >> return 0;
    >>
    >> }
    >>
    >> best regards,
    >> Tony Winslow

    >
    > That's so cool... I'm going to use it to rate how important my
    > function calls are now... 5 stars is really mega-important... 1 is
    > kind-of "who cares"...
    > ...


    Except that if won't work with overloaded functions. Those will have to
    remain "unrated"...
     
    Andrey Tarasevich, Sep 8, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren
    Replies:
    0
    Views:
    517
    Darren
    Oct 11, 2004
  2. Dude
    Replies:
    0
    Views:
    374
  3. ce
    Replies:
    1
    Views:
    3,013
  4. sam pal
    Replies:
    3
    Views:
    585
    E. Robert Tisdale
    Jul 16, 2003
  5. SanPy
    Replies:
    6
    Views:
    864
    Gabriel Genellina
    Oct 3, 2007
Loading...

Share This Page