confused with typedefs

Discussion in 'C Programming' started by raashid bhatt, Jun 27, 2009.

  1. typedef int function(int);

    Please explain how can i use this one
     
    raashid bhatt, Jun 27, 2009
    #1
    1. Advertising

  2. raashid bhatt

    Eric Sosman Guest

    raashid bhatt wrote:
    > typedef int function(int);
    >
    > Please explain how can i use this one


    There's not much you can do with this one. You can
    use it in the declaration of a function pointer:

    int this(int x) { return x + 1; }
    int that(int y) { return y - 1; }
    function *fptr = this;
    function *gptr = that;

    .... but that's about the extent of it. You cannot use
    it in the actual definition of a function taking one int
    parameter and returning an int value:

    function this { return x + 1; } /* no good */

    .... because there's no way the typedef can supply the name
    of the parameter (no, not even if you write a name in the
    typedef itself).

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 27, 2009
    #2
    1. Advertising

  3. raashid bhatt

    jameskuyper Guest

    Eric Sosman wrote:
    > raashid bhatt wrote:
    > > typedef int function(int);
    > >
    > > Please explain how can i use this one

    >
    > There's not much you can do with this one. You can
    > use it in the declaration of a function pointer:


    You can also use it in a declaration of a function:

    function this;
    function that;

    Note that this can be useful if inserted before definitions like the
    ones below, because any mismatch between the function definition and
    the function declaration will require a diagnostic message.

    > int this(int x) { return x + 1; }
    > int that(int y) { return y - 1; }

    ....
     
    jameskuyper, Jun 27, 2009
    #3
  4. jameskuyper <> writes:

    > Eric Sosman wrote:
    >> raashid bhatt wrote:
    >> > typedef int function(int);
    >> >
    >> > Please explain how can i use this one

    >>
    >> There's not much you can do with this one. You can
    >> use it in the declaration of a function pointer:

    >
    > You can also use it in a declaration of a function:
    >
    > function this;
    > function that;


    and it can be used to declare a function parameter where is will be
    adjusted to a pointer type (so this is not really another way to use
    it all!):

    int call_it(function f, int i) { return f(i); }

    This style is odd enough that it should probably be discouraged.

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Jun 28, 2009
    #4
    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. Alexander Stippler

    Re: visibility of typedefs

    Alexander Stippler, Jun 24, 2003, in forum: C++
    Replies:
    5
    Views:
    823
    tom_usenet
    Jun 25, 2003
  2. emerth
    Replies:
    3
    Views:
    379
    emerth
    Aug 8, 2003
  3. dwrayment

    Templates and Typedefs

    dwrayment, Aug 12, 2003, in forum: C++
    Replies:
    6
    Views:
    401
    Sam Holden
    Aug 14, 2003
  4. raashid bhatt

    confused with typedefs

    raashid bhatt, Jun 27, 2009, in forum: C Programming
    Replies:
    0
    Views:
    250
    raashid bhatt
    Jun 27, 2009
  5. raashid bhatt

    confused with typedefs

    raashid bhatt, Jun 27, 2009, in forum: C Programming
    Replies:
    1
    Views:
    305
    Nick Keighley
    Jun 29, 2009
Loading...

Share This Page