Comparison of typedef int (*funp_t) (int) and typedef int fun_t (int)

Source: Internet
Author: User

Similarity point
    • All use typedef to assign a simple name to a complex combination of types consisting of the underlying data type;
    • All follow the tradition of C--the type defined by typedef ends with "_t";
    • Both can play a role in reducing the amount of code character input;
    • "To some extent" increases the readability of the code (this view is not absolutely correct,
    • The calling function is used in the same way:
secondfirstsecond(fun_t *firstsecondfirst);

For these three ways, within the second function, you can invoke either first (14) or (*first) (14).

Difference
    • typedef int (*funp_t) (int) Defines a function pointer type funp_t; and typedef int (fun_t) (int) defines a function type fun_t;
//定义一个类型是int (*)(int//声明一个整型参数和一个整型返回值的函数p,即:int p(int*p//定义一个指向函数int ()(int)的指针变量p和funp_t p;等价
    • Funp_t can be directly used for function parameters and return values, whereas fun_t can be used directly for function parameters, but not directly for function return values, which are combined with * to be used as return values;
setfun(funp_t nf)//right 设置一个新的函数指针nf,返回旧的函数指针getfun();//error,函数不能返回函数getfun()//rightsetfun(fun_t nf)//right 等价于 funp_t nfsetfun(fun_t *nf)//right 等价于 funp_t nf

C99 "Explicit" indicates that a function cannot return a function (the 137th pin callout of ISO/IEC 9899:1999 P141); functions cannot be defined inside functions, and functions can be converted to corresponding function pointers (ISO/IEC 9899:1999 P143 EXAMPLE2). otherwise not!

    • funp_t use more, and fun_t use less!

For fun_t effects In addition to combining ' * ' declarations or defining variables. It also has an occasional useful use: function internal (external) Declaration Function!
Although C99 indicates that a function without a static limit has an implicit extern property, some compilers do not: it is still declared (defined) and reused, so if a function is called before it is declared or defined, you must declare it explicitly before calling it!
For example, the following code slice

void second(void)//first定义在后,且无前置声明{    first(14);//某些编译器会编译不过或给出警告}int first(int n){    printf("%d\n", n);    return7;}

(1) can be declared before the call to resolve

fun_t first;//函数声明void second(void){    fun_t first; //函数声明    first(14);}//...

Can be declared more than once, but only once. The above code is declared two times, just for example, the user can choose one. The main purpose here is to reflect the function inside the functions can also be declared way!
(2) Solve by passing parameter form

void second(fun_t first){    first(14);    (*first)(14); //right}//...

Comparison of typedef int (*funp_t) (int) and typedef int fun_t (int)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.