void (*func)(int(*[ ])());
6 に答える
毛むくじゃらの宣言子を読み取るための一般的な手順は、左端の識別子を見つけて、それを思い出して前[]
に()
バインドすることです*
(つまり、*a[]
配列へのポインターではなく、ポインターの配列です)。このケースは、パラメーター リストに識別子がないため、少し難しくなりますが、 の[]
前にバインドされる*
ため、*[]
がポインターの配列を示していることがわかります。
だから、与えられた
void (*func)(int(*[ ])());
次のように分類します。
func -- func
*func -- is a pointer
(*func)( ) -- to a function taking
(*func)( [ ] ) -- an array
(*func)( *[ ] ) -- of pointers
(*func)( (*[ ])()) -- to functions taking
-- an unspecified number of parameters
(*func)(int(*[ ])()) -- returning int
void (*func)(int(*[ ])()); -- and returning void
これが実際にどのように見えるかは、次のようになります。
/**
* Define the functions that will be part of the function array
*/
int foo() { int i; ...; return i; }
int bar() { int j; ...; return j; }
int baz() { int k; ...; return k; }
/**
* Define a function that takes the array of pointers to functions
*/
void blurga(int (*fa[])())
{
int i;
int x;
for (i = 0; fa[i] != NULL; i++)
{
x = fa[i](); /* or x = (*fa[i])(); */
...
}
}
...
/**
* Declare and initialize an array of pointers to functions returning int
*/
int (*funcArray[])() = {foo, bar, baz, NULL};
/**
* Declare our function pointer
*/
void (*func)(int(*[ ])());
/**
* Assign the function pointer
*/
func = blurga;
/**
* Call the function "blurga" through the function pointer "func"
*/
func(funcArray); /* or (*func)(funcArray); */
それは声明ではなく、宣言です。
func
これは、voidを返し、型の単一の引数をとる関数へのポインターとして宣言しますint (*[])()
。これ自体は、intを返し、固定されているが指定されていない数の引数をとる関数へのポインターです。
少しの信仰のあなたがたのためのcdecl出力:
cdecl> explain void (*f)(int(*[ ])());
declare f as pointer to function (array of pointer to function returning int) returning void
はい:
$ cdecl void(* x)(int(* [])());を説明します。 xを関数へのポインタとして宣言する (intを返す関数へのポインタの配列)voidを返す
void (*func)(blah);
blah
は引数を取る関数へのポインタblah
です。ここでint(*[ ])()
、は関数ポインタの配列です。
C宣言を読むためのガイドは次のとおりです。
http://www.ericgiguere.com/articles/reading-c-declarations.html
Geordi
これをトレーニングできる C++ ボットです。
<litb> geordi: {} void (*func)(int(*[ ])());
<litb> geordi: -r << ETYPE_DESC(func)
<geordi> lvalue pointer to a function taking a pointer to a pointer to a nullary function
returning an integer and returning nothing
すべてのパラメーター宣言を表示するなど、多くの便利なことができます (実際、これは生のC++
文法規則名に一致するだけです)。
<litb> geordi: show parameter-declarations
<geordi> `int(*[ ])()`.
反対方向にやってみましょう:
<litb> geordi: {} int func;
<litb> geordi: make func a pointer to function returning void and taking array of pointer to
functions returning int
<litb> geordi: show
<geordi> {} void (* func)(int(*[])());
要求すれば、与えられたものは何でも実行します。訓練を受けていても、かっこの恐ろしいルールをいくつか忘れてしまった場合は、C++ と geordi スタイルの型記述を混在させることもできます。
<litb> geordi: make func a (function returning void and taking (int(*)()) []) *
<geordi> {} void (* func)(int(*[])());
楽しむ!