这个函数的参数在哪里?

时间:2013-08-13 17:00:29

标签: c++ c syntax declaration

void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d);

返回类型是void *,proto_type是函数的名称?或者这是一个功能指针?参数是什么:

(long int, char *b)或此(const char *b, unsigned short int d)

请解释此功能的工作原理。

8 个答案:

答案 0 :(得分:6)

void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d);

这是一个功能声明。函数名称为proto_type,它采用两种类型的参数:

  • long int
  • char *

并返回指向

的函数的指针
  • const char*
  • unsigned short int

并返回void*

现在,如果您使用typedef,以上所有内容都会变得明显:

typedef void* function_type(const char *b, unsigned short int d);

function_type* proto_type(long int, char *b);

希望有所帮助。

答案 1 :(得分:5)

这声明proto_type作为函数接受(long int, char *)并返回指向函数的指针,该函数接受(const char *, unsigned short int)并返回一个void指针。

换句话说:

typedef void * (*R)(const char *, unsigned short int);   // return type

R proto_type(long int, char *);

答案 2 :(得分:2)

我建议为此类问题添加书签www.cdecl.org。请注意,页面对于您输入的内容有点挑剔(它至少不接受参数名称)。

答案 3 :(得分:2)

从函数名称开始,然后使用该函数进行评估 通常的表达式语法。因此:

proto_type                  //  proto_type 
proto_type( long, char* )   //  is a function taking long, char*
                            //  (since function call has precedence over *
*proto_type( long, char* )  //  returning a pointer
(*proto_type(...))          //  (no change, but necessary for precedence reasons)
(...)( char const*, unsigned short )
                            //  to a function taking char const*, unsigned short
*(...)(...)                 //  returning pointer
void *(...)(...)            //  to void

(如果你没有这个名字,会有点困难, 喜欢static_cast中的类型。)

答案 4 :(得分:2)

我将解释我解析复杂c风格声明的过程。我希望你会发现它很有用。

C风格的解析可以被认为是从标识符开始并由括号限定的从右到左的解析。所以我们从标识符proto_type

开始
//       vvvvvvvvvv-------------------
void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d);

正如我们所看到的,从proto_type向右移动,我们立即看到一个函数调用和一些参数,所以这很容易。 proto_type是一个以long intchar*为参数的函数。

//                                   v
void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d);

这里我们点击第一个边界括号,所以我们必须向后找到下一个标记:

//      v----------------------------
void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d);

所以现在我们知道它会返回指向某个东西的指针。现在我们必须再次向右看,以便再次找出由于括号括起来的内容

//     v--------------------------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d);

由于我们发现了另一个函数调用操作符,它返回的指针必须是一个指向const char*unsigned short int的函数的指针。

最后,我们到达了另一个边界括号,因此向左移动以找到该函数指针的返回值。

//vvvv----------------------------------------------------------------------v
void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d);

函数指针返回void *作为返回值。

所以回顾一下英语:proto_typelong intchar*作为参数,并返回一个指向const char*和{{1}的函数的指针}作为参数并返回unsigned short int

答案 5 :(得分:1)

proto_type是一个函数,它带有两个参数long intchar *。它返回一个函数指针,它接受类型为const char *unsigned short int的两个参数。

而不使用typedef来写这个对读者来说并不是很友善。

答案 6 :(得分:1)

在Windows 8 x64上使用VS2012。

proto_type是一个带long int(未命名)和unsigned short int d的函数,并返回void * proto2(const char *b, unsigned short int d);

形式的函数

这是一些有效的代码:

#include <stddef.h>

void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d);
void * proto2(const char *b, unsigned short int d);

int main(int argc, char** argv){
    if(proto_type(0, NULL)(NULL, 0) != (void*)8675309)
        return 0;
    else
        return 1;
}

void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d){
    return &proto2;
}

void* proto2(const char *b, unsigned short int d){
    return (void*)8675309;
}

正在发生的是main调用proto_type,它会返回指向proto2的指针,main会立即调用。 proto2会返回我们在main中检查的魔术指针值。

这是VS2012的调试输出:

The program '[3188] test.exe' has exited with code 1 (0x1).

至于“这个函数是如何工作的”,答案是你想要的(假设你是实现它的那个)。

如果你想用另一种方式来看它,你想要从里到外读出它来调用哪些函数的顺序。

这里的教训是C(++)函数指针非常不透明。

答案 7 :(得分:1)

我已经完成了@Tomek所说的并将您的原型复制到http:www.cdel.org,然后从原型中删除了参数名称:

    void * (*proto_type(long int, char *)) (const char *, unsigned short int );

按回车键,网站返回:

declare proto_type as function (long int, pointer to char) returning pointer
to function (pointer to const char, unsigned short int) returning pointer
to void

现在,将它转换为碳单元说:proto_type被声明为一个函数,它将long int和一个指向char的指针作为参数,它返回一个指向函数的指针指向const charunsigned short integer的指针,该函数返回void pointer

最后,void pointer可以转换为指向任何其他数据类型的指针。