tags:

views:

142

answers:

2

What are the rules? OTOH the simple case seems to imply the new type is the last thing on a line. Like here Uchar is the new type.

typedef unsigned char Uchar;

But a function pointer is completely different. Here the new type is pFunc:

typedef int (*pFunc) (int);

I can't think of any other examples offhand but I have come across some very confusing usages.

So are there rules or are people just suppose to know from experience that this is how it is done because they have seen it done this way before?

ALSO: What is the scope of a typedef.

Thanks to everyone.

+12  A: 

Basically a typedef has exactly the same syntax as an object declaration except that it is prefixed with typedef and that changes the meaning so that the new identifier declares an alias for the type that the object that would have been declared had it been a normal declaration would have had.

A typedef is scoped exactly as the object declaration would have been, so it can be file scoped or local to a block or (in C++) to a namespace or class.

e.g.

Declares an int:

int a;

Declares a type that is an alias for int:

typedef int a_type;

Declares a pointer to a char:

char *p;

Declares an alias for a char *

typedef char *pChar;

Declares a function pointer:

int (*pFn)(int);

Declares an alias for the type that is 'pointer to a function taking int and returning int'.

typedef int (*pFunc)(int);
Charles Bailey
It also works for any kind of simple-declaration (not only for object declarations). Also for function declarations: `typedef void foo();`
Johannes Schaub - litb
Brilliant; shows the consistency of the syntax where the OP could not see it.
Clifford
+1, best explanation I've ever seen.
Evan Teran
@litb: Absolutely true. I was ignoring it for simplicity.
Charles Bailey
Thank you. That's clear. It appears I made things way more complicated than they really are.
unknown google user
+1  A: 

This is covered and answered in Chapter 6, section 6.7 "Typedef" page 146, and A8.9 "Typedef" of Appendix A page 221, of The C Programming Language, 2nd ed. by Brian W. Kernighan and Dennis M. Ritchie (K&R).

A more comprehensive answer can also be found in C: A Reference Manual, 5th ed., by Samuel P. Harbison III and Guy L. Steele Jr. in Chapter 5, Section 5.10.

These two books are taken as the two references used by C programmers, other than the international ISO (9899:1999, aka C99) standards themselves. I would strongly advocate that you get one, if not both of these references for your own use.

mctylr
Somehow I feel like "buy one of these books" is not a valid answer.
JonM
unknown google user
@JonM, I didn't say the OP had to buy. I _do_ recommend obtaining them, whether buying, or borrowing such as from friends, employer, or libraries. And in particular, I do cite the _specific sections_ of the books that answer the OP question, better than I can.
mctylr
ArunSaha
The authors and publisher of The C Programming Language have _not_ made it freely available online.
mctylr