About typedef -- define the function pointer or define function model?

Discussion in 'C Programming' started by robin liu, Apr 21, 2006.

  1. robin liu

    robin liu Guest

    What's the difference between these two declarations ?

    1) typedef void (*pf)(void);
    2) typedef void f(void);

    the first declaration is define a function pointer, what is the second ?
    define a function model?

    And can use the second declaration to define a function pointer as follow:

    typedef void f(void);
    f *pf;

    Whether the two declarations of pf1 are both pointer to a function?

    1) typedef void (*pf)(void);
    pf pf1;
    2) typedef void f(void);
    f *pf1;

    if yes, which is better? and I saw many codes used the second declaration,
    what is its' advantage or disadvantage?


    Thanks in advance!



    from china
    robin liu, Apr 21, 2006
    #1
    1. Advertising

  2. robin liu said:

    > What's the difference between these two declarations ?
    >
    > 1) typedef void (*pf)(void);
    > 2) typedef void f(void);
    >
    > the first declaration is define a function pointer,


    No, it defines a synonym for an existing function pointer type.

    > what is the second ? define a function model?


    No, it defines a synonym for an existing function type.

    > And can use the second declaration to define a function pointer as follow:
    >
    > typedef void f(void);
    > f *pf;


    Yes.

    > Whether the two declarations of pf1 are both pointer to a function?
    >
    > 1) typedef void (*pf)(void);
    > pf pf1;
    > 2) typedef void f(void);
    > f *pf1;


    Yes.

    > if yes, which is better? and I saw many codes used the second declaration,
    > what is its' advantage or disadvantage?


    The advantage of the second is that it does not hide a pointer
    characteristic inside a typedef. The * reminds us that here be dragons,
    which can be a useful hint. So I use the second form.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Apr 21, 2006
    #2
    1. Advertising

  3. robin liu

    robin liu Guest

    Thank Richard very much!!

    And your explaination is very detailed and clear. According to it, I think
    I have understand the different between the two declarations
    and I believe I can use them better from now.

    Thanks again!!


    "Richard Heathfield" <> ????
    news:...
    > robin liu said:
    >
    > > What's the difference between these two declarations ?
    > >
    > > 1) typedef void (*pf)(void);
    > > 2) typedef void f(void);
    > >
    > > the first declaration is define a function pointer,

    >
    > No, it defines a synonym for an existing function pointer type.
    >
    > > what is the second ? define a function model?

    >
    > No, it defines a synonym for an existing function type.
    >
    > > And can use the second declaration to define a function pointer as

    follow:
    > >
    > > typedef void f(void);
    > > f *pf;

    >
    > Yes.
    >
    > > Whether the two declarations of pf1 are both pointer to a function?
    > >
    > > 1) typedef void (*pf)(void);
    > > pf pf1;
    > > 2) typedef void f(void);
    > > f *pf1;

    >
    > Yes.
    >
    > > if yes, which is better? and I saw many codes used the second

    declaration,
    > > what is its' advantage or disadvantage?

    >
    > The advantage of the second is that it does not hide a pointer
    > characteristic inside a typedef. The * reminds us that here be dragons,
    > which can be a useful hint. So I use the second form.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
    robin liu, Apr 21, 2006
    #3
  4. robin liu

    Robin Liu Guest

    After review my post news, I found that I make a big mistake.

    Thanks to richard, you are right. The first declaration I said defines a
    synonym for an existing function
    pointer type, and the second defines a synonym for an existing function
    type.

    Thanks and apologize for my mistake once again !!




    "Richard Heathfield" <> ????
    news:...
    > robin liu said:
    >
    > > What's the difference between these two declarations ?
    > >
    > > 1) typedef void (*pf)(void);
    > > 2) typedef void f(void);
    > >
    > > the first declaration is define a function pointer,

    >
    > No, it defines a synonym for an existing function pointer type.
    >
    > > what is the second ? define a function model?

    >
    > No, it defines a synonym for an existing function type.
    >
    > > And can use the second declaration to define a function pointer as

    follow:
    > >
    > > typedef void f(void);
    > > f *pf;

    >
    > Yes.
    >
    > > Whether the two declarations of pf1 are both pointer to a function?
    > >
    > > 1) typedef void (*pf)(void);
    > > pf pf1;
    > > 2) typedef void f(void);
    > > f *pf1;

    >
    > Yes.
    >
    > > if yes, which is better? and I saw many codes used the second

    declaration,
    > > what is its' advantage or disadvantage?

    >
    > The advantage of the second is that it does not hide a pointer
    > characteristic inside a typedef. The * reminds us that here be dragons,
    > which can be a useful hint. So I use the second form.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
    Robin Liu, Apr 21, 2006
    #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. Replies:
    1
    Views:
    469
    Andreas Wollschlaeger
    Oct 6, 2006
  2. Cancerbero

    typedef function pointer

    Cancerbero, Sep 12, 2004, in forum: C Programming
    Replies:
    5
    Views:
    665
    Joona I Palaste
    Sep 14, 2004
  3. O Plameras
    Replies:
    10
    Views:
    533
    Keith Thompson
    Dec 19, 2005
  4. Replies:
    2
    Views:
    416
    Aleksey Loginov
    Nov 7, 2005
  5. oor
    Replies:
    0
    Views:
    1,324
Loading...

Share This Page