18.1.6 Type Definitions (typedef)

[This section corresponds to K&R Sec. 6.7]

When the storage class is typedef, as in

	typedef int count;
a declaration means something completely different than it usually does. Instead of declaring a variable named count, we are declaring a new type named count. (Actually, we're just declaring a new name for an old type; you can think of typedef names as type aliases.) Having declared this new type count, we can now use it as a base type in other declarations, such as
	count napples, noranges;

The types FILE and size_t which we've seen at various points along the way (which we've described as being ``new types defined by the header file <stdio.h>'' [or also several other headers in the case of size_t]) are typically both defined using typedef.

You can use typedef to define new names for complicated types, too. You could define

	typedef char *string;
	typedef struct listnode list, *nodeptr;
after which you can declare several strings (char *'s) by saying
	string string1, string2;
or several lists (struct listnode) by saying
	list list1, list2;
or several pointers to list nodes by saying
	nodeptr np1, np2;

typedef provides a way to simplify structure declarations. In a previous section, we saw that we had to declare new variables of type struct complex using the syntax

	struct complex c1, c2;
Using typedef, however, we can introduce a single-word complex type, after all:
	typedef struct complex complextype;
	complextype c1, c2;
It's also possible to define a structure and a typedef tag for it at the same time:
	typedef struct complex
		{
		double real;
		double imag;
		} complextype;
Furthermore, when using typedef names, you may not need the structure tag at all; you can also write
	typedef struct
		{
		double real;
		double imag;
		} complextype;
(At this point, of course, you could use the cleaner name ``complex'' for the typedef, instead of ``complextype''. Actually, it turns out that you could have done this all along. Structure tags and typedef names share separate namespaces, so the declaration
	typedef struct complex
		{
		double real;
		double imag;
		} complex;
is legal, though possibly confusing.)

Defining new type names is done mostly for convenience, or to make the code more self-documenting, or to make it possible to change the actual base type used for a lot of variables without rewriting the declarations of all those variables.

A typedef declaration is a little bit like a preprocessor #define directive. We could imagine writing

	#define count int
	#define string char *
in an attempt to accomplish the same thing. This won't work nearly as well, however: given the macro definition, the line
	string string1, string2;
would expand to
	char * string1, string2;
which would declare string1 as a char * but string2 as a plain char. The typedef declaration, however, would work correctly.

Some programmers capitalize typedef names to make them stand out a little better, and others use the convention of ending all typedef names with the characters ``_t''.


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1996-1999 // mail feedback