[英]Function pointers, arrays and lvalues in C
Let's suppose that we have the following functions (in C): 假设我们有以下函数(在C中):
int sum(int a, int b){
return a+b;
}
int diff(int a, int b){
return a-b;
}
So we know that we can declare an array of funtion pointers in the following way: 所以我们知道我们可以通过以下方式声明一个函数指针数组:
int (*test[2]) (int a, int b);
test[0] = sum;
test[1] = diff;
But the following is also valid (but we use heap allocation): 但以下也有效(但我们使用堆分配):
int (**test) (int a, int b) = malloc( 2*sizeof(*test));
test[0] = sum;
test[1] = diff;
So far so good. 到现在为止还挺好。 Now let remember that to declare a (dynamically allocated) array of two integers we can do:
现在让我们记住,我们可以做一个(动态分配的)两个整数的数组:
int* test = malloc( 2*sizeof(int));
So why we cannot declare an array of function pointers as 那么为什么我们不能将一个函数指针数组声明为
int (*test) (int a, int b) = malloc( 2*sizeof(*test)); ?
Is the reason that as test is the same as *test
and **test
(and so on), malloc( 2*sizeof(*test))
is returning a pointer to a function pointer and therefore it cannot be assigned to (*test)
? 是因为测试与
*test
和**test
(等等)相同, malloc( 2*sizeof(*test))
返回指向函数指针的指针,因此无法将其指定为(*test)
?
If this supposition is correct, can you explain in detail why we get the compilation error 如果这个假设是正确的,你能详细解释为什么我们得到编译错误
error: lvalue required as left operand of assignment
when we try to do 当我们尝试做的时候
int (*test) (int a, int b) = malloc( 2*sizeof(*test));
test=diff; //<--- This is ok.
test+1 = sum; //<--- This is what gives the error!
Disclaimer: I suppose that this is a basic question and the supposition is correct, but I would like a better explanation to have this kind of thing clear one and for all. 免责声明:我认为这是一个基本问题,假设是正确的,但我希望有一个更好的解释,让这种事情一劳永逸。
Edit: 编辑:
Notice that this is equivalent to 请注意,这相当于
int (*test) (int a, int b) = malloc( 2*sizeof(*test));
*test=*diff; //<--- This is ok.
*(test+1) = *sum; //<--- This is what gives the error!
as this is somewhat more similar to the case: 因为这与案件有点类似:
int *test = malloc(2*sizeof(*test));
*test = 0;
*(test+1) = 1;
So why we cannot declare an array of function pointers as
那么为什么我们不能将一个函数指针数组声明为
int (*test) (int a, int b) = malloc( 2*sizeof(*test));
Because test
does not point to a function pointer; 因为
test
不指向函数指针; it is a function pointer. 它是一个函数指针。 Thus it cannot point to the first element of an array of function pointers.
因此,它不能指向函数指针数组的第一个元素。
If you want an array of function pointers use the previous form: 如果你想要一个函数指针数组,请使用前一个表单:
int (**test) (int a, int b) = malloc( 2*sizeof(*test));
Here, *test
has function pointer type and thus test
can (and does) point to the first element of an array of function pointers. 这里,
*test
有函数指针类型,因此test
可以(并且确实)指向函数指针数组的第一个元素。 Further: 进一步:
error: lvalue required as left operand of assignment
when we try to do
当我们尝试做的时候
int (*test) (int a, int b) = malloc( 2*sizeof(*test)); test=diff; //<--- This is ok. test+1 = sum; //<--- This is what gives the error!
No matter what type test
has, test+1=anything
is always invalid C. test+1
can never be an lvalue. 无论什么类型的
test
, test+1=anything
总是无效的C. test+1
永远不会是左值。 I don't see why you would expect this to work. 我不明白为什么你会期望这个工作。
GCC is also papering over another bug in your program, sizeof(*test)
. GCC还在报道程序中的另一个错误
sizeof(*test)
。 Since *test
has function type, sizeof(*test)
is invalid, but GCC silently assigns it the value 1. This results in allocating too little memory for a function pointer, but it doesn't matter anyway because in the following line you throw away the memory you got from malloc
and assign something else to test
. 由于
*test
有函数类型, sizeof(*test)
无效,但是GCC默默地为它赋值1.这导致为函数指针分配的内存太少,但无论如何都没关系,因为在下面的行中你抛出远离你从malloc
获得的malloc
并分配其他东西进行test
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.