以下C声明的含义

以下C声明的含义,c,pointers,C,Pointers,我无法理解下面的声明,有人能帮我一下吗。这是有点混乱,我找不到任何适当的解释这个表达加上我从来没有见过这样的声明类型在我的实践之前 char(*(*x())[])(); char(*(*x[3])())[5]; void (*b(int, void (*f)(int)))(int); void (*ptr)(int(*)[2], int(*)(void)); 这看起来像是一个邪恶的例子,直接来自Kernighan和Ritchie(C编程语言)的第二版,p。122(第5.12节:复杂声明),其中

我无法理解下面的声明,有人能帮我一下吗。这是有点混乱,我找不到任何适当的解释这个表达加上我从来没有见过这样的声明类型在我的实践之前

char(*(*x())[])();
char(*(*x[3])())[5];
void (*b(int, void (*f)(int)))(int);
void (*ptr)(int(*)[2], int(*)(void));

这看起来像是一个邪恶的例子,直接来自Kernighan和Ritchie(C编程语言)的第二版,p。122(第5.12节:复杂声明),其中它被描述为一个函数,返回指向指向返回char的函数的指针数组的指针。下面是一个使用示例:

#include <stdio.h>

char x1() { return 'a'; } // Function returning a char
char (*x2[])() = {&x1}; // Array of pointers to functions returning  char
char (*(*x())[])() { return &x2; } // Function returning a pointer to the above

void main(){
    char (*x3)() = **x(); // Pointer to a function returning char
    printf("This is the value: %c\n", x3());
}
#包括
char x1(){return'a';}//返回字符的函数
char(*x2[])()={&x1};//指向返回char的函数的指针数组
char(**x())[])(){return&x2;}//返回指向上面的指针的函数
void main(){
char(*x3)(=**x();//指向返回char的函数的指针
printf(“这是值:%c\n”,x3());
}

您需要对它们进行分解,后两个更容易:

char (*(*x())[])();
char (*x)();        // A pointer to a function returning char with any args.
char (*x[])();      // An array of such pointers.
char (*(*x)[])();   // A pointer to such array.
char (*(*x())[])(); // A function returning such pointer: A function returning a pointer to
                    // an array of pointer to function returning char with any args.

char(*(*x[3])())[5];
char(*x)();          // A pointer to a function returning char with any args.
char(*x)()[5];       // A pointer to a function returning an array of 5 chars (any args).
char((*x)())[5];     // Same as above.
char(*(*x)())[5];    // A pointer to a function returning a pointer to an array of 5 chars.
char(*(*x[3])())[5]; // An array of 3 of these pointers.

void (*b(int, void (*f)(int)))(int);
X b(int, void(*f)(int)); // A function returning X and taking an int and a pointer to
                         // a function f taking an int and returning nothing.
void (*b())(int); // A function taking nothing and returning a pointer to a function
                  // taking and int and returning nothing.
void (*b(int, void (*f)(int)))(int); // Combination of the two above, a function taking and
                                     // an int and a pointer to a function f and returning
                                     // a pointer to a function.

void(*ptr)(int(*)[2], int(*)(void)); // This one is easier:
void(*ptr)(); // A  pointer to a function with no args and returning nothing.
void(*ptr)(int(*)[2], int(*)(void)); // A pointer to a function returning nothing and taking:
                                     // - A pointer to an array of 2 int
                                     // - A pointer to a function returning int with no args.
你可以用电脑检查所有这些

需要分解此类声明时的一些提示:
  • 返回指向数组或函数指针的函数并不常见,但您必须知道如何声明它们:
  • 请注意,在这两个声明中,返回类型的一部分出现在函数的参数列表之后,这就是为什么当您第一次遇到它们时,这些返回类型经常会混淆的原因

  • 始终可以删除函数中参数的名称:
  • 如果名称紧邻方括号,例如
    x[N]
    ,则它是一个«数组N of something»:

  • @tkausl那个网站很烂。它只识别出上面4行中的2行,即使这4行都是编译的。。。他们鼓励的解决方案不是用typedef演示如何避免这种不可读的表达式(这将是一件简单的事情),而是编写自己的表达式解析器来理解混乱。真是太愚蠢了。
    int (*f())[5];   // A function returning a pointer to an array of 5 int.
    int (*f())(int); // A function returning a pointer to a function int (*)(int).
    
    int b(void(*f)(int), int(*p)[2]);
    int b(void(*)(int), int(*)[2]); // Same as above
    
    int (*x[3])();  // x is an array of 3 pointers to functions int(*)().
    int (*x[4])[5]; // x is an array of 4 pointers to array of 5 int.
    int (*x)[4];    // x is not an array.