以匿名配置文件作为参数的复杂类型声明
Complex type declaration with an anonymous profile as a parameter
我读了一本关于 C 语言编程的俄语书籍。我运行进了一个问题。有这样类型的函数:
int (*replace_f(int (*func)(int, int)))(int, int)
该函数得到一个类型为"int (int, int)"的函数指针,returns一个相同类型函数的指针。重要的一点是,这两个标识符 (replace_f,func) 之前都没有在代码中的任何地方声明过。
这是一个正确的声明,我理解。但是,作者声称如果我们需要声明指向早期函数的类型指针,我们必须声明为:
int (*(*ptr_replace)(int ( * )(int, int)))(int, int);
not as
int (*(*ptr_replace)(int ( *func )(int, int)))(int, int);
or
int (*(*ptr_replace)(int ( *any_other_identifier )(int, int)))(int, int);
也就是说,我们肯定要写一个匿名类型int(*)(int, int))
但是任何案例都可以正常工作并成功编译。
他声称在类型声明中使用两个或多个标识符是不可用的。为什么它会起作用?
在 int (*replace_f(int (*func)(int, int)))(int, int);
中,func
的声明具有 函数原型作用域 因为它出现在函数声明中。所以这个声明声明了两个标识符,replace_f
和func
,但是func
的范围在函数声明的末尾结束。
这使得 func
在这种情况下毫无用处。在某些情况下函数原型范围内的标识符是有用的,例如void foo(int x, int y, float a[x][y]);
,其中x
和y
用于描述a
的维度。
But then, the author claims if we need to declare the type pointer to the early function we have to declare as: … not as …
这是不正确的。您可以用所示三种方式中的任何一种声明 ptr_replace
。
He claims that it's not available to use two or more identifiers in the type declaration. Why does it work then?
声明replace_f
确实有效。声明 func
或 any_other_identifier
不起作用。所以也许原文是这样的意思?
我读了一本关于 C 语言编程的俄语书籍。我运行进了一个问题。有这样类型的函数:
int (*replace_f(int (*func)(int, int)))(int, int)
该函数得到一个类型为"int (int, int)"的函数指针,returns一个相同类型函数的指针。重要的一点是,这两个标识符 (replace_f,func) 之前都没有在代码中的任何地方声明过。 这是一个正确的声明,我理解。但是,作者声称如果我们需要声明指向早期函数的类型指针,我们必须声明为:
int (*(*ptr_replace)(int ( * )(int, int)))(int, int);
not as
int (*(*ptr_replace)(int ( *func )(int, int)))(int, int);
or
int (*(*ptr_replace)(int ( *any_other_identifier )(int, int)))(int, int);
也就是说,我们肯定要写一个匿名类型int(*)(int, int)) 但是任何案例都可以正常工作并成功编译。 他声称在类型声明中使用两个或多个标识符是不可用的。为什么它会起作用?
在 int (*replace_f(int (*func)(int, int)))(int, int);
中,func
的声明具有 函数原型作用域 因为它出现在函数声明中。所以这个声明声明了两个标识符,replace_f
和func
,但是func
的范围在函数声明的末尾结束。
这使得 func
在这种情况下毫无用处。在某些情况下函数原型范围内的标识符是有用的,例如void foo(int x, int y, float a[x][y]);
,其中x
和y
用于描述a
的维度。
But then, the author claims if we need to declare the type pointer to the early function we have to declare as: … not as …
这是不正确的。您可以用所示三种方式中的任何一种声明 ptr_replace
。
He claims that it's not available to use two or more identifiers in the type declaration. Why does it work then?
声明replace_f
确实有效。声明 func
或 any_other_identifier
不起作用。所以也许原文是这样的意思?