简体   繁体   English

以下 C 声明的含义

[英]Meaning of the following C declarations

I am unable to understand the following declaration can somebody please help me with this.我无法理解以下声明,有人可以帮我解决这个问题吗? It is bit confusing and I can't find any proper explanation for this expression plus I have never seen such type of declaration before in my practice.这有点令人困惑,我找不到对这个表达式的任何正确解释,而且我以前在实践中从未见过这种类型的声明。

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

You need to decompose them, the last two are easier:您需要分解它们,最后两个更容易:

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.

You can check all these using cdecl.org .您可以使用cdecl.org检查所有这些。

Some tips when you need to decompose such declarations:需要分解此类声明时的一些提示:

  1. A function returning a pointer to an array or to a function is not common, but you have to know how to declare them:返回指向数组或函数的指针的函数并不常见,但您必须知道如何声明它们:
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).

Notice that within these 2 declarations, part of the return type appears after the parameter list of the function, which is why those are often confusing when you encounter them for the first time.请注意,在这 2 个声明中,部分返回类型出现函数的参数列表之后,这就是为什么当您第一次遇到它们时经常会感到困惑的原因。

  1. You can always remove the name of parameters in function:您始终可以删除函数中的参数名称:
int b(void(*f)(int), int(*p)[2]);
int b(void(*)(int), int(*)[2]); // Same as above
  1. If the name is directly next to a square-bracket, eg x[N] , then it is an « array N of something »:如果名称直接在方括号旁边,例如x[N] ,那么它是一个«数组 N 的东西»:
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.

This looks like an evil example straight from the 2nd edition of Kernighan and Ritchie (The C Programming Language), p.这看起来像是直接来自 Kernighan 和 Ritchie(C 编程语言),第 2 版的邪恶例子。 122 (section 5.12: Complicated Declarations), where it is described as a function returning a pointer to an array of pointers to functions returning char. 122(第 5.12 节:复杂声明),其中它被描述为一个函数,该函数返回一个指向返回 char 的函数的指针数组的指针。 Here is a usage example:这是一个使用示例:

#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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM