小结:
1、
typedef并没有创建一个新类型,它只是为某个已存在的类型增加了一个新的名称而已;
2、
typedef声明也没有证据新的语义:通过这种方式声明的变量与通过普通方式声明的变量具有完全相同的属性;
3、
类似于#define,但typedef是由编译器解释的,因此它的文本替换供功能要超过预处理器的功能;
4、
typedef可以使程序参数化,提高程序的可移植性。如果typedef声明的数据类型同机器有关,那么,当程序移植到其他机器上时,只需该表typedef类型定义就可以了。
一个经常用到的情况是,对于各种不同大小的整型值来说,都使用通过typedef定义的类型名,然后,分别为各个不同放入宿主机选择一组合适的short、int和logn类型大小即可。
标准库中有一些例子,例如size_t和ptrdiff_t等。
6.7 Typedef
C provides a facility called typedef for creating new data type names. For example, the declaration
typedef int Length;
makes the name Length a synonym for int. The type Length can be used in declarations, casts, etc., in
exactly the same ways that the int type can be:
Length len, maxlen;
Length *lengths[];
Similarly, the declaration
typedef char *String;
makes String a synonym for char * or character pointer, which may then be used in declarations and
casts:
String p, lineptr[MAXLINES], alloc(int);
int strcmp(String, String);
p = (String) malloc(100);
Notice that the type being declared in a typedef appears in the position of a variable name, not right after
the word typedef. Syntactically, typedef is like the storage classes extern, static, etc. We have
used capitalized names for typedefs, to make them stand out.
The C programming Language
6.7 Typedef 133
As a more complicated example, we could make typedefs for the tree nodes shown earlier in this chapter:
typedef struct tnode *Treeptr;
typedef struct tnode { /* the tree node: */
char *word; /* points to the text */
int count; /* number of occurrences */
struct tnode *left; /* left child */
struct tnode *right; /* right child */
} Treenode;
This creates two new type keywords called Treenode (a structure) and Treeptr (a pointer to the
structure). Then the routine talloc could become
Treeptr talloc(void)
{
return (Treeptr) malloc(sizeof(Treenode));
}
It must be emphasized that a typedef declaration does not create a new type in any sense; it merely adds a
new name for some existing type. Nor are there any new semantics: variables declared this way have exactly
the same properties as variables whose declarations are spelled out explicitly. In effect, typedef is like
#define, except that since it is interpreted by the compiler, it can cope with textual substitutions that are
beyond the capabilities of the preprocessor. For example,
typedef int (*PFI)(char *, char *);
creates the type PFI, for ``pointer to function (of two char * arguments) returning int,‘‘ which can be used
in contexts like
PFI strcmp, numcmp;
in the sort program of Chapter 5.
Besides purely aesthetic issues, there are two main reasons for using typedefs. The first is to parameterize a
program against portability problems. If typedefs are used for data types that may be machine−dependent,
only the typedefs need change when the program is moved. One common situation is to use typedef
names for various integer quantities, then make an appropriate set of choices of short, int, and long for
each host machine. Types like size_t and ptrdiff_t from the standard library are examples.
The second purpose of typedefs is to provide better documentation for a program − a type called Treeptr
may be easier to understand than one declared only as a pointer to a complicated structure.
FROM
The C programming Language By Brian W. Kernighan and Dennis M. Ritchie.
typedef define typedef可以使程序参数化,提高程序的可移植性。
原文:https://www.cnblogs.com/yuanjiangw/p/10500658.html