I am trying to implement an interpreter. I'd love to go with GCC first class labels to make it threaded code, but I should hold on to a standard this time, so naturally I am left with function table. So, I'm doing this:
我正在尝试实现一个解释器。我喜欢使用GCC的第一类标签来制作线程代码,但这次我应该坚持一个标准,所以我自然会留下功能表。所以,我这样做:
unsigned short int FUN_TABLE[MAX_FUN] (void*);
And I want to fill it with functions, each getting pointer to its operands, doing its part, returning length of the whole instruction in memory to a dispatcher.
我想用函数填充它们,每个函数都获得指向其操作数的指针,完成它的工作,将整个指令的长度返回给调度程序。
The thing is, I can't even compile it due to the following error: declaration of FUN_TABLE as array of functions
. Considering it is exactly what I am trying to achieve, why is this an error, why should I pay it attention, and if I shouldn't, how to suppress it in elegant and standardized manner?
问题是,由于以下错误,我甚至无法编译它:声明FUN_TABLE作为函数数组。考虑到这正是我想要实现的,为什么这是一个错误,我为什么要关注它,如果我不应该,如何以优雅和标准化的方式压制它?
3 个解决方案
#1
2
You can define an array of function pointers like this (pseudocode):
你可以像这样定义一个函数指针数组(伪代码):
int (*funcArr2[10])(param, param, ...) = {NULL};
However, you should be aware that this means that all these functions have the same set of arguments. You can not declare an array with function pointers to totall different functions with regard to their signature.
但是,您应该知道这意味着所有这些函数都具有相同的参数集。你不能声明一个带有函数指针的数组,以便根据它们的签名来完成不同的函数。
#2
2
GCC is telling you: "there is no such thing as an array of functions".
海湾合作委员会告诉你:“没有一系列功能”。
Considering it is exactly what I am trying to achieve, why is this an error, why should I pay it attention
考虑到这正是我想要实现的,为什么这是一个错误,为什么我要注意它
Because you are trying to achieve something that does not exist in the C language. But instead, you can achieve the desired functionality through an array of function pointers.
因为您正在尝试实现C语言中不存在的内容。但相反,您可以通过一系列函数指针实现所需的功能。
The syntax of declaring a function pointer is
声明函数指针的语法是
return_type (*func_ptr_name)(parameters)
and the syntax for declaring an array of function pointers is
和声明函数指针数组的语法是
return_type (*func_ptr_name[n])(parameters)
Since that syntax is quite obscure, you will not want to use it. The solution is to use typedefs:
由于该语法非常模糊,因此您不希望使用它。解决方案是使用typedef:
typedef unsigned short (*func_table_t)(void*);
// declare an array of function pointers, using readable syntax:
func_table_t func_table [MAX_FUNC] =
{
&some_function,
&some_other_function,
...
};
#3
1
Arrays of functions aren't legal. Your easiest work around would be an array of pointers to functions -- but this implies that each function being pointed to from the array has the same signature.
功能阵列不合法。最简单的解决方法是指向函数的指针数组 - 但这意味着从数组中指向的每个函数都具有相同的签名。
#1
2
You can define an array of function pointers like this (pseudocode):
你可以像这样定义一个函数指针数组(伪代码):
int (*funcArr2[10])(param, param, ...) = {NULL};
However, you should be aware that this means that all these functions have the same set of arguments. You can not declare an array with function pointers to totall different functions with regard to their signature.
但是,您应该知道这意味着所有这些函数都具有相同的参数集。你不能声明一个带有函数指针的数组,以便根据它们的签名来完成不同的函数。
#2
2
GCC is telling you: "there is no such thing as an array of functions".
海湾合作委员会告诉你:“没有一系列功能”。
Considering it is exactly what I am trying to achieve, why is this an error, why should I pay it attention
考虑到这正是我想要实现的,为什么这是一个错误,为什么我要注意它
Because you are trying to achieve something that does not exist in the C language. But instead, you can achieve the desired functionality through an array of function pointers.
因为您正在尝试实现C语言中不存在的内容。但相反,您可以通过一系列函数指针实现所需的功能。
The syntax of declaring a function pointer is
声明函数指针的语法是
return_type (*func_ptr_name)(parameters)
and the syntax for declaring an array of function pointers is
和声明函数指针数组的语法是
return_type (*func_ptr_name[n])(parameters)
Since that syntax is quite obscure, you will not want to use it. The solution is to use typedefs:
由于该语法非常模糊,因此您不希望使用它。解决方案是使用typedef:
typedef unsigned short (*func_table_t)(void*);
// declare an array of function pointers, using readable syntax:
func_table_t func_table [MAX_FUNC] =
{
&some_function,
&some_other_function,
...
};
#3
1
Arrays of functions aren't legal. Your easiest work around would be an array of pointers to functions -- but this implies that each function being pointed to from the array has the same signature.
功能阵列不合法。最简单的解决方法是指向函数的指针数组 - 但这意味着从数组中指向的每个函数都具有相同的签名。