prev up next   top/contents search

comp.lang.c FAQ list · Question 1.13

Q: What's the difference between using a typedef or a #define for a user-defined type?

A: In general, typedefs are preferred, in part because they can correctly encode pointer types. For example, consider these declarations:

	typedef char *String_t;
	#define String_d char *
	String_t s1, s2;
	String_d s3, s4;
s1, s2, and s3 are all declared as char *, but s4 is declared as a char, which is probably not the intention. (See also question 1.5.)

#defines do have the advantage that #ifdef works on them (see also question 10.15). On the other hand, typedefs have the advantage that they obey scope rules (that is, they can be declared local to a function or block).

See also questions 1.17, 2.22, 11.11, and 15.11.

References: K&R1 Sec. 6.9 p. 141
K&R2 Sec. 6.7 pp. 146-7
CT&P Sec. 6.4 pp. 83-4

prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North