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

R

robin liu

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!



(e-mail address removed) from china
 
R

Richard Heathfield

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.
 
R

robin liu

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!!
 
R

Robin Liu

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 !!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top