需要左值作为函数指针的赋值左操作数 [英] lvalue required as left operand of assignment for function pointers

查看:403
本文介绍了需要左值作为函数指针的赋值左操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将函数分配给函数指针,但是出现以下错误:

I am trying to assign a function to a function pointer, but I am getting the following error:

需要左值作为赋值的左操作数.

lvalue required as left operand of assignment.

我的代码如下:

#include <stdio.h>
void intr_handler(int param){
    printf("Hey there!\n");
}

int main(){
    void *(intr_handlerptr)(int);
    intr_handlerptr = intr_handler;
}

我看不到那里是什么问题,因为我为指针"intr_handlerptr"分配了函数"intr_handler",并且它们具有相同的签名.我想念什么?

I cannot see what is the problem there, since I am assigning to the pointer "intr_handlerptr" the function "intr_handler" and they have the same signature. What am I missing?

推荐答案

我不确定void *(intr_handlerptr)(int);的作用(尽管它可以编译,但是对此单独提出另一个问题现在很有趣,现在是

I'm unsure of what void *(intr_handlerptr)(int); does (it compiles, though, would be interesting to ask another question for this alone which is now done) but this declaration is incorrect. It should be:

void (*intr_handlerptr)(int);

然后您的代码正确编译

关于函数指针的教程: https://www.cprogramming.com/tutorial/function-pointers.html

tutorial on function pointers: https://www.cprogramming.com/tutorial/function-pointers.html

在讨论此语法错误之后,似乎很明显(现在!)

after discussion about this syntax error it seems obvious (now!) that

void *(intr_handlerptr)(int);

与以下相同:

void *intr_handlerptr(int);

因此向前声明一个函数(该函数不存在,因此不会链接,但是由于编译器在尝试为其分配内容时出现错误,因此您看不到它)

so forward declaring a function (which doesn't exist so it would not link, but you can't see that as the compiler issues an error when trying to assign something to it)

这篇关于需要左值作为函数指针的赋值左操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆