typedef 精髓

本文转载自《https://tonybai.com/2008/05/02/also-talk-about-typedef/》

核心阐述

C语言语法简单,但内涵却博大精深;如果在学习时只是止步于表面,那么往往后期会遇到很多困难。typedef是C语言中一个很好用的工具,大量存在于已有代码中,特别值得一提的是:C++标准库实现中更是对typedef有着大量的使用。但很多初学者对其的理解仅局限于:typedef用来定义一个已有类型的”别名(alias)”。正是因为有了这样的理解,才有了后来初学者在typedef int myint和typedef myint int之间的犹豫不决。很多国内大学的C语言课之授课老师也都是如是说的,或者老师讲的不够透彻,导致学生们都是如是理解的。我这里想结合C语言标准文档以及一些代码实例,也说说typedef。

1
int    *p;

这样的代码是C语言中最最基础的一个语句了,大家都知道这个语句声明了一个变量p,其类型是指向整型的指针(pointer to int);如果在这个声明的前面加上一个typedef后,整个语义(semantics)又会是如何改变的呢?

1
typedef  int    *p;

我们先来看看C99标准中关于typedef是如何诠释的?C99标准中这样一小段精辟的描述:”In a declaration whose storage-class specifier is typedef, each declarator defines an identifier to be a typedef name that denotes the type specified for the identifier in the way described in xx”。

参照这段描述,并拿typedef int *p作为例子来理解:

在一个声明中,如果有存储类说明符typedef的修饰,标识符p将被定义为了一个typedef name,这个typedef name表示(denotes)一个类型,什么类型呢?就是int *p这个声明(declarator)中标识符(indentifier)p的类型(int*)。

再比对一下两个声明:

1
2
int    *p;
typedef int *p;

是不是有点”茅舍顿开”的感觉,int *p中, p是一个变量,其类型为pointer to int;在int *p前面增加一个 typedef后,p变为一个typedef-name,这个 typedef-name 所表示的类型就是 int *p 声明式中 p 的类型(int*)
说句白话,typedefp 去除了普通变量的身份,摇身一变,变成了 p 的类型的一个typedef-name了。

为了巩固上面的理解,我们再来看看”C语言参考手册(C: A Reference Manual)”中的说法:任何 declarator (如typedef int *p )中的indentifier(如p)定义为typedef-name, 其(指代p)表示的类型是declarator为正常变量声明(指代int p)的那个标识符(指代p)的类型(int)。有些绕嘴,不过有例子支撑:

示例

示例1

typedef double MYDOUBLE;

分析:
去掉 typedef , 得到正常变量声明=> double MYDOUBLE;,
变量MYDOUBLE 的类型为 double;
由此可推导出 => typedef double MYDOUBLE;MYDOUBLE 是类型 double 的一个 typedef-name

如: MYDOUBLE d; // 说明 d 是一个 double 类型的变量

示例2

typedef double *Dp;

分析:
去掉 typedef, 得到正常变量声明 double *Dp;,
变量 Dp 的类型为 double*,即 pointer to double;
由此可推导出 => typedef double *Dp;Dp 是类型 double* 的一个 typedef-name

如: Dp dptr; // dptr是一个pointer to double的变量, 和double * dptr; 是同等含义

示例3

typedef int* Func(int);

分析:
去掉 typedef, 得到正常变量声明 int* Func(int);,
变量 Func 的类型为一个函数标识符,该函数返回值类型为 int*,参数类型为 int ;
由此可推导出 => typedef int* Func(int)Func函数类型(函数返回值类型为int*,参数类型为int)的一个 typedef-name

如: Func *fptr; // fptr是一个pointer to function with one int parameter, returning a pointer to int
如: Func f; 这样的声明意义就不大了。

示例4

typedef int (*PFunc)(int);

分析:
去掉 typedef,得到正常变量声明 => int (*PFunc)(int);
变量PFunc 的类型为一个函数指针,指向的返回值类型为 int参数类型为int的函数原型;

由此可推导出 => “typedef int (*PFunc)(int)”中PFunc是函数指针类型(该指针类型指向返回值类型为int,参数类型为int的函数)的一个typedef-name。

PFunc fptr; <=> fptr是一个pointer to function with one int parameter, returning int

示例5

typedef int A[5];

分析:
去掉 typedef,得到正常变量声明 => int A[5];
变量A的类型为一个含有5个元素的整型数组;

由此可推导出 => “typedef int A[5]“中A是含有5个元素的数组类型的一个typedef-name。

A a = {3, 4, 5, 7, 8};
A b = { 3, 4, 5, 7, 8, 9}; /* 会给出Warning: excess elements in array initializer */

示例6

typedef int (*A)[5]; (注意与typedef int* A[5]; 区分)

分析:
去掉 typedef,得到正常变量声明 => int (*A)[5];
变量A的类型为pointer to an array with 5 int elements;

由此可推导出 => “typedef int (*A)[5]“中A是”pointer to an array with 5 int elements”的一个typedef-name。

int c[5] = {3, 4, 5, 7, 8};
A a = &c;
printf(“%d\n”, (a)[0]); / output: 3 */

如果这样赋值:
int c[6] = {3, 4, 5, 7, 8, 9};
A a = &c; /* 会有Warning: initialization from incompatible pointer type */

示例7

typedef struct _Foo_t Foo_t;

分析:
去掉 typedef,得到正常变量声明 => struct _Foo_t Foo_t;
变量Foo_t的类型为struct _Foo_t;

由此可推导出 => “typedef struct _Foo_t Foo_t”中Foo_t是”struct _Foo_t”的一个typedef-name。

示例8

typedef struct { … // } Foo_t;

分析:
去掉 typedef,得到正常变量声明 => struct { … // } Foo_t;
变量Foo_t的类型为struct { … // } ;

由此可推导出 => “typedef struct { … // } Foo_t “中Foo_t是”struct { … // }”的一个typedef-name。这里struct {…//}是一个无”标志名称(tag name)”的结构体声明。

示例9

typedef struct { … // } Foo_t[1];

分析:
去掉 typedef, 得到正常变量声明 => struct { … // } Foo_t[1];
变量Foo_t的类型为包含一个元素的struct { … // }类别的数组类型;

由此可推导出 => 这样一来,Foo_t在typedef定义后实际上就变成了一个struct { … // }数组类型。要问实际编程中会这么用typedef吗?你还别说,这还是C语言常用的一个小技巧,如果你有机会看到jmp_buf的类型定义,你就会发现jmp_buf在很多系统实现中也是如此定义的,大约类似:typedef struct XX {…} jmp_buf[1]; 这样做的目的大致是这样的:如果你在函数里定义了一个char a[n];那么a和&a作为参数传入某个函数时是等价的。看似传值,实则传址,在被调用函数中通过参数可直接修改数组a的元素的内容。另外这么做的目的是否是为了让代码更符合某些人的口味我还不得而知。

参考资料:

1、”ISOIEC-98991999(E)–Programming Languages–C”之Page 123;
2、C语言参考手册(中文版) 之 Page 119


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!