typedef declaration

R

raj_iv

typedef function pointers
typedef int (*pFunc1) (int, int);

typedef int (*)(int,int) pFunc2;

pFunc1 p1; // This is OK

pFunc2 p2; // This is wrong

Why is the second way of typedef invalid, any specific reasons?
 
S

Seebs

typedef function pointers
typedef int (*pFunc1) (int, int);

typedef int (*)(int,int) pFunc2;

pFunc1 p1; // This is OK

pFunc2 p2; // This is wrong

Why is the second way of typedef invalid, any specific reasons?

Uh, because it doesn't match C's declaration syntax?

I don't understand the question.

You might as well write:

int i;
=3

"why is the second way of initialize invalid"? Because that's not
how the syntax works.

-s
 
C

chrisbazley

typedef function pointers
typedef int (*pFunc1) (int, int);

typedef int (*)(int,int) pFunc2;

pFunc1 p1; // This is OK

pFunc2 p2; // This is wrong

Why is the second way of typedef invalid, any specific reasons?

I think I can give a more helpful answer than Peter's, because I can
understand where you are coming from.

The reason is that C's type definition syntax is designed to resemble
declarations, which are in turn designed to resemble usage. The 'name'
part of 'typedef int name;' isn't at the end of the statement because
it is a type definition - it is at the end because you would declare
an object of type 'int' as 'int name'. However, the name-at-end
pattern doesn't apply to array and function type declarations. You
wouldn't declare a function pointer as 'int (*) (int, int) name;' (nor
call it as 'foo = (bar, baz) name;') so you can't define a function
type using that syntax either!
 
S

Seebs

I think I can give a more helpful answer than Peter's, because I can
understand where you are coming from.
The reason is that C's type definition syntax is designed to resemble
declarations, which are in turn designed to resemble usage.

Oh!

I get it.

Good explanation.

More examples to consider:

int foo; => "foo" is an int.
int *foo; => "*foo" is an int.
int foo[3]; => "foo[x]" is an int.
int (*foo)(int); => "(*foo)(x)" is an int.

-s
 

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

Forum statistics

Threads
474,104
Messages
2,570,643
Members
47,246
Latest member
rangas

Latest Threads

Top