By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!

ro*********@hotmail.com from china
Apr 21 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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)
Apr 21 '06 #2

P: n/a
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" <in*****@invalid.invalid> ????
news:LK******************************@bt.com...
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)

Apr 21 '06 #3

P: n/a
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" <in*****@invalid.invalid> ????
news:LK******************************@bt.com...
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)

Apr 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.