typedef void foo_t()

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

    #1

    typedef void foo_t()

    How does typedef work for foo2_t?

    What can one do with foo2_t?

    ------ bar.c ------
    void foo() {}

    typedef void (*foo1_t)();
    typedef void foo2_t(); /* How does typedef work here? */

    int main()
    {
    foo1_t foo1 = foo;
    foo2_t foo2 = foo; /* Line 9 */
    return 0;
    }
    -------------------

    GNU gcc 4.0.1

    bar.c: In function `main':
    bar.c:9: error: function `foo2' is initialized like a variable


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com

    http://sourceforge.net/users/alexvn





  • Richard Tobin

    #2
    Re: typedef void foo_t()

    In article <dnbrtt$1ee$1@d omitilla.aioe.o rg>,
    Alex Vinokur <alexvn@users.s ourceforge.net> wrote:
    [color=blue]
    >typedef void foo2_t(); /* How does typedef work here? */[/color]
    ....[color=blue]
    >foo2_t foo2 = foo; /* Line 9 */[/color]

    This would be legal:

    foo2_t *foo2 = foo;

    -- Richard

    Comment

    • Richard Heathfield

      #3
      Re: typedef void foo_t()

      Alex Vinokur said:
      [color=blue]
      > How does typedef work for foo2_t?
      >
      > What can one do with foo2_t?
      >
      > ------ bar.c ------
      > void foo() {}
      >
      > typedef void (*foo1_t)();[/color]

      This means that

      foo1_t x;

      is a declaration that x is a pointer to a function taking unknown arguments
      and returning void.
      [color=blue]
      > typedef void foo2_t(); /* How does typedef work here? */[/color]

      This means that

      foo2_t y;

      is a declaration that y is a function taking unknown arguments and returning
      void.

      Because it's a function, it can't be instantiated in the same way an object
      can. Nevertheless, this is a useful technique. If the topic cops can just
      hold their fire for a second...

      typedef LRESULT RJH_MESSAGE_HAN DLER(HWND,
      UINT,
      WPARAM,
      LPARAM);

      RJH_MESSAGE_HAN DLER DebuggerCreate;
      RJH_MESSAGE_HAN DLER DebuggerDestroy ;
      RJH_MESSAGE_HAN DLER DebuggerReset;
      RJH_MESSAGE_HAN DLER DebuggerDisplay ;

      is valid C, and (arguably) is neater than:

      LRESULT DebuggerCreate( HWND, UINT, WPARAM, LPARAM);
      LRESULT DebuggerDestroy (HWND, UINT, WPARAM, LPARAM);
      LRESULT DebuggerReset(H WND, UINT, WPARAM, LPARAM);
      LRESULT DebuggerDisplay (HWND, UINT, WPARAM, LPARAM);

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)

      Comment

      • Niklas Norrthon

        #4
        Re: typedef void foo_t()

        Richard Heathfield <invalid@invali d.invalid> writes:
        [color=blue]
        > Alex Vinokur said:
        >[color=green]
        > > How does typedef work for foo2_t?
        > >
        > > What can one do with foo2_t?
        > >
        > > ------ bar.c ------
        > > void foo() {}
        > >
        > > typedef void (*foo1_t)();[/color]
        >
        > This means that
        >
        > foo1_t x;
        >
        > is a declaration that x is a pointer to a function taking unknown arguments
        > and returning void.
        >[color=green]
        > > typedef void foo2_t(); /* How does typedef work here? */[/color]
        >
        > This means that
        >
        > foo2_t y;
        >
        > is a declaration that y is a function taking unknown arguments and returning
        > void.
        >
        > Because it's a function, it can't be instantiated in the same way an object
        > can. Nevertheless, this is a useful technique. If the topic cops can just
        > hold their fire for a second...[/color]

        I don't see anything in what you write that could be considered off topic, so
        no need to ask the topic cops to shut up... (And I haven't written one single
        line of code for windows apps in more than five years).
        [color=blue]
        > typedef LRESULT RJH_MESSAGE_HAN DLER(HWND,
        > UINT,
        > WPARAM,
        > LPARAM);
        >
        > RJH_MESSAGE_HAN DLER DebuggerCreate;
        > RJH_MESSAGE_HAN DLER DebuggerDestroy ;
        > RJH_MESSAGE_HAN DLER DebuggerReset;
        > RJH_MESSAGE_HAN DLER DebuggerDisplay ;[/color]

        Interesting. I've never used function declaration like these, and I agree that
        they could be prefered in some situations...

        /Niklas Norrthon

        Comment

        • Richard Heathfield

          #5
          Re: typedef void foo_t()

          Niklas Norrthon said:
          [color=blue]
          > Richard Heathfield <invalid@invali d.invalid> writes:
          >[color=green]
          >> typedef LRESULT RJH_MESSAGE_HAN DLER(HWND,
          >> UINT,
          >> WPARAM,
          >> LPARAM);
          >>
          >> RJH_MESSAGE_HAN DLER DebuggerCreate;
          >> RJH_MESSAGE_HAN DLER DebuggerDestroy ;
          >> RJH_MESSAGE_HAN DLER DebuggerReset;
          >> RJH_MESSAGE_HAN DLER DebuggerDisplay ;[/color]
          >
          > Interesting. I've never used function declaration like these, and I agree
          > that they could be prefered in some situations...[/color]


          I forgot to say earlier that I actually prefer to typedef function types
          rather than function pointer types, because:

          void foo(RJH_MESSAGE _HANDLER *fptr)
          {
          ... whatever ...
          }

          reminds me that I'm dealing with a pointer type. I don't like hiding
          pointers with typedef. Just a style thing really.


          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at above domain (but drop the www, obviously)

          Comment

          Working...