简体   繁体   English

指向函数指针数组的指针

[英]pointer to an array of pointer to functions

I would like f to point to an array of f_pointer_type. 我想f指向f_pointer_type的数组。 So what did i miss here ? 那我在这里想念什么? I'm getting an error 'lvalue required as left operand of assignment'. 我收到错误消息“需要左值作为赋值的左操作数”。 What does it mean ? 这是什么意思 ?

#include <stdio.h>
typedef char* (*f_pointer_type)();
char * retHello()
{
    return "hello";
}
int main()
{
    char (* ( *f())[])(); //Declare f to be a pointer to an array of pointers to function that gets nothing and return a char.
    f_pointer_type funcs[3]; //an array of pointers to function that gets nothing and returns a pointer to char
    f = &funcs; //ERROR : lvalue required as left operand of assignment
    return 0;
}

If you read about the clockwise/spiral rule you will see that f is actually a function returning a pointer to an array of pointers to functions returning char* . 如果您了解了顺时针/螺旋规则,您会发现f实际上是一个返回指针的函数,该指针指向返回char*的函数的指针数组。 In other words it's a function prototype and not a variable declaration. 换句话说,它是函数原型,而不是变量声明。

Try this instead: 尝试以下方法:

f_pointer_type (*f)[];

An array of function pointers is written as char* (*f[])() . 函数指针数组写为char* (*f[])() You have the parenthesis wrong. 您的括号写错了。

Though it is of course better to always use typedef as in the f_pointer_type funcs[3] example. 尽管最f_pointer_type funcs[3]示例中那样始终使用typedef。

You define f as a function pointer. 您将f定义为function pointer. f is pointer to a function who has void params and return char * f是指向具有void paramsreturn char *的函数的指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM