理解typedef

理解typedef,关键是要把typedef后面的内容当作一个整体,然后即可得到typedef所定义关键字的类型,无外乎变量和函数指针。

下面是在stackoverflow上摘录的一个关于typedef定义函数指针的问答

问:

I'm learning how to dynamically load DLL's but what I don't understand is this line
typedef void (*FunctionFunc)();
I have a few questions. If someone is able answer them I would be grateful.
Why is typedef used?
The syntax looks odd; after void should there not be a function name or something? It looks like an anonymous function.
Is a function pointer created to store the memory address of a function?
So I'm confused at the moment; can you clarify things for me?


答:

typedef is a language construct that associates a keyword to a type.
You use it the same way you would use the initial type, for instance

  typedef int myinteger;
  typedef char *mystring;
  typedef void (*myfunc)();


using them like

  myinteger i;   // is equivalent to    int i;
  mystring s;    // is the same as      char *s;
  myfunc f;      // compile equally as  void (*f)();
As you can see, you could just replace the typedefed keyword with its definition given above.

The difficulty lies in the pointer to functions syntax and readability in C and C++, and the typedef can improve the readability of such declarations. However, the syntax is appropriate, since functions - unlike other simpler types - may have a return value and parameters, thus the need to use more keywords and parentheses to write the function declaration.
The readability may start to be really tricky with pointers to functions arrays, and some other even more indirect flavors.
To answer your three questions
Why is typedef used? To ease the reading of the code - especially for pointers to functions, or structure names.
The syntax looks odd (in the pointer to function declaration) That syntax is not obvious to read, at least when beginning. Using a typedef declaration instead eases the reading
Is a function pointer created to store the memory address of a function? Yes, a function pointer stores the address of a function. This has nothing to do with the typedef construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the actual code.
Exemple:


typedef int (*t_somefunc)(int,int);
int square(int u, int v) {
  return u*v;
}
t_somefunc afunc = □
...
int x2 = (*afunc)(123, 456); // call square() to calculate 123*456


上边三个typedef解释如下:

 typedef int myinteger;

int myinteget;myinterger的类型是整形,那么以后可以用myinterger来声明整形变量,如:myinterger inter,相当于把int myinterger中的myinterger替换为inter,即int  inter,inter就是一个整形变量。

  typedef char *mystring;

char *mystring;mystring类型是字符指针,以后可以用mystring来定义字符指针,如:mystring str,相当于把char *mystring中的mystring替换为str,即char *str,str就是一个字符指针。

  typedef void (*myfunc)();

void (*myfunc)();myfunc类型是函数指针,这个函数指针没有形参,返回值为空,以后可以用myfunc来定义函数指针,如:myfunc fun,相当于把void (*myfunc)()中的myfunc替换为fun,即void (*fun)(),fun就是一个函数指针。


总的来说,只要看typedef后面定义变量是什么类型即可


linux中有一个信号函数的声明如下,可以看出,这个函数的声明非常的晦涩难懂

void (*signal(int signo, void(*func)(int)))(int);
但是这个声明可以拆分成如下形式,就比较好懂了

typedef void (*sighandler_t)(int);

sighandler_t signal(int signo, sighandler_t handler);

或者用APUE上的形式,

typedef void Sigfunc(int);

Sigfunc *signal(int , Sigfunc *)

对这个函数的解释,还是看一个来自stackoverflow的解释吧,比较清晰

it's a function that takes two arguments, an integer and a pointer to a function that takes an integer as an argument and returns nothing, and it (signal()) returns a pointer to a function that takes an integer as an argument and returns nothing.

这个signal函数有两个参数,第一个参数是int类型,第二个参数是函数指针类型,这个函数指针有一个int类型的参数,返回值为空,而signal函数返回值类型为函数指针,这个函数指针有一个int类型的形参,返回值为空!!!

分析C语言中的声明,《C专家编程》中有比较好的方法,如下图所示:


下面转一个cu上的讲解

int (*p)();
这是一个函数指针, p所指向的函数是一个不带任何参数, 并且返回值为int的一个函数.
int (*fun())();
这个式子与上面式子的区别在于用fun()代替了p,而fun()是一个函数,所以说就可以看成是fun()这个函数执行之后,它的返回值是一个函数指针,这个函数指针(其实就是上面的p)所指向的函数是一个不带任何参数,并且返回值为int的一个函数.所以说对于

void (*signal(int signo, void (*fun)(int)))(int);

就可以看成是signal()函数(它自己是带两个参数,一个为整型,一个为函数指针的函数), 而这个signal()函数的返回值也为一个函数指针,这个函数指针指向一个带一个整型参数,并且返回值为void的一个函数.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值