Complex function pointer declared with the C language

The contents of this article described herein are applicable to the C language, C ++ also refer to

Here is a declaration involves complex function pointers

int (*Register (int (*pf)(const char *, const char *))) (const char *, const char *);

Many small partners might have kept, whether it is in the statement what? I can tell you the answer first:

This line declares a program called Register offunction, Register functionReturns a pointer to the functionParameter is a function pointer, the parameter name pf.
The same function pointer Register function returns function pointer type and the type of the parameter list, there are two point type parameter const char *, int return value of a function.

How to deal with this complex statement in C language code?

If you can, for the first time declared repeatedly stated that dismantling

About a statement and repeated statements, please refer to my other article: a statement in the C language and repeatedly stated

For each statement after the dismantling, analyzed according to the following method

  1. From left to right, skipping and type the name of the operator, to find the first non-type name words, this is the identifier is declared, assuming he called A

    In the above code, the first non-word type name is Register, so this line of code declares an identifier called the Register

    Note : Not all of the C language type name in the keyword, as well as some custom. For example struct pire is a type name, but the pire is defined by the user. Highlights are the following code as a type name. (The following three lines unrelated)

    struct x {int a;} x; // be customized to the type of structure struct x, then reference the structure type struct x, the first term is a non-type name x
    struct {int a;} s; // be customized to an unnamed structure type struct {int a;}, and then refer to this type of structure, the first non-type words of s name
    struct d y; // reference the structure type struct d has been defined, the first term is a non-type name y

  2. Find the name, we come to type.

    A first look at whether there is left '(' if not, skip ②;. If, after determining the following steps skip to ④.

     A 的左边有 '(' 说明 A 被这个括号与另外一个部分绑定在了一起。我们只要找到这个部分,就能知道 A 的实质是什么。
     找到与 A 左边的括弧相对应的括弧,看看这个括号中除了 A 之外的部分是什么形式(也就是括号中 A 的右边那部分),
     会有以下2种情况:
     	1) 方括号型:例如 (A[20])。方括号型说明 A 是一个数组。
     	2) 圆括号型:例如 (A(int a))。圆括号型说明 A 是一个函数,A 右边的这个括号就是 A 的参数列表
    

    A look at whether there are the right ').' If no, skip to ③; if, after determining the following steps skip to ④.

     A 的右边有 ')' 说明 A 被这个括号与另外一个部分绑定在了一起。我们只要找到这个部分,就能知道 A 的实质是什么。
     找到与 A 右边的括弧相对应的括弧,看看这个括号中除了 A 之外的部分是什么形式(也就是括号中 A 的左边那部分),
     只可能有以下1种情况:
     	星型:A 的左边部分是个 * 号。这说明 A 是个指针
    

    A two sides are not parentheses, to see both sides so what about A. ④ After the jump is determined according to the following steps.

     1) 先看右边是否紧挨着 '[',如果是,那么 A 就跟这个方括号以及方括号中的内容绑定在一起,说明 A 是个数组
     2) 如果右边没有方括号,看看右边是否紧挨着 '(',如果是,那么 A 就跟这个圆括号以及圆括号中的内容绑定在一起,
      	说明 A 是个函数,这个圆括号内就是 A 的参数列表
     3) 如果 A 右边没有 '(' 也没有 '[' ,那么看看 A 左边是否紧挨着 * 号,如果是,那么 A 就跟这个 * 号绑定在一起,
     	说明 A 是个指针
     4) 如果上述3种情况都不符合,那么不用继续往下看了,我相信这一定是一条最简单的声明语句
    

    If you manage to get to this point, we have successfully know the nature of A's. A is either an array or a pointer, or a function. We have been told A
    that part bound together (an array of square brackets, parentheses are functions, pointers are asterisk) and A as a whole, called the first part. Analyzing then continues as follows:

    1, first, if the left next to the first portion '(', next to the right side ''), the first portion of the outer pair of brackets is useless, it will be incorporated into the first portion. Repeat this step until the first portion of the outer bracket useless absent

    Two, then we begin determining the type of the first part (of the type herein refers to the type or types of array elements of the type of function return value, pointer):

    Third, to see what about the first part:

     1) 如果有[ ]就说明第一部分的类型是数组,把[ ]收纳进来,形成第二部分
     2) 如果没有[ ]就看看是否有( ),如果有,说明第一部分的类型是函数,把( )收纳进来,形成第二部分
     3) 如果既没有[ ]也没有( ),就看看是否有 * 号,如果有,说明第一部分的类型是指针,把 * 号收纳进来,形成第二部分
     4) 如果上述3种都没有,就只可能剩下如 int,float 之类的类型名,这就是第一部分的类型,如果是这种情况,复杂声明
     	的判断就已经结束了
    

    If the determination has not ended, the second portion is formed, then repeating the above three steps, if it is judged still not completed, a third portion is formed, and repeating the above three steps, until the end determination

  3. Here, judgment is over. I believe that smart you have fully understood it. If you do not understand, then re-read this article, pay more try to figure out, will become immortal. I had fully understood the students can return to the beginning of the article to practice with the first line of code, come to see if the answer is the correct answer.

  4. For students not yet fully understood, the following are marked with different colors when determining the various parts of the divided codes beginning of the article, hoping to help.
    (Yellow for the first portion, the second portion of the black frame, the third part is underlined)

Code portions schematic

  1. For parameter function's parameter list declared, we can be down to every method in accordance with this article, and finally you can successfully understand the intricate chaos miscellaneous code

  2. Fixed format parameter list in the function pointer declaration of no identifier parameter, only keywords and operators, e.g. start code appears in the article four times const char *, by the process of this case can not be determined in this article What is the parameter type. In this case, there are the following two solutions:

     1) 一般这种情况下的声明的类型都比较简单,聪明的你一定一眼就能看出来
     2) 多做解读复杂声明的练习,到时定会灵光一现,恍然大悟
    

At last


#define 欢迎小伙伴们留言~

#include 转载请注明出处哦

https://blog.csdn.net/weixin_43737206/article/details/99239833

fprintf(如果你觉得有用的话, "点个好看再走吧 %s" , "嘿嘿");

Published 19 original articles · won praise 23 · views 3899

Guess you like

Origin blog.csdn.net/weixin_43737206/article/details/99239833