Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > About typedef -- define the function pointer or define function model?

Reply
Thread Tools

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

 
 
robin liu
Guest
Posts: n/a
 
      04-21-2006
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


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      04-21-2006
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)
 
Reply With Quote
 
 
 
 
robin liu
Guest
Posts: n/a
 
      04-21-2006
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:LKadne-...
> 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)



 
Reply With Quote
 
Robin Liu
Guest
Posts: n/a
 
      04-21-2006
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:LKadne-...
> 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)



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
#define and typedef ramu C Programming 2 01-18-2006 07:39 AM
Operational and functional differences - #define vs typedef ? O Plameras C Programming 10 12-19-2005 08:29 AM
Can I use typedef to define types used in the return type in template function? PengYu.UT@gmail.com C++ 2 11-07-2005 03:01 PM
typedef function pointer Cancerbero C Programming 5 09-14-2004 08:37 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57