登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

张继飞的博客

呜呼哀哉

 
 
 

日志

 
 

再谈typedef(重点为函数指针)  

2009-09-17 10:15:43|  分类: linux学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

有种很方便的写法。

typedef int *p;

p pointer;

这时直接把pointer带入原式中,取代p然后去掉typedef,得到的结果就是int * pointer;

哈哈,这样直接替换就很直观多了。

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

int    *p;

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

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*)。

再比对一下两个声明:

int    *p;

typedef  int    *p;

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

[例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

#include "iostream"

using namespace std;

int add(int a,int b){
return (a+b);
}

typedef int (* func)(int ,int ) ;

void main(){
func f = add;
int n = f(1,2);
cout << n << endl;
}

[例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)"的结构体声明。

参考资料:

1、"ISOIEC-98991999(E)--Programming Languages--C"之Page 123;

2、C语言参考手册(中文版) 之 Page 119

  评论这张
 
阅读(4826)| 评论(6)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018