lvalue required as left operand of assignment for function pointers

纵饮孤独 提交于 2019-12-10 18:17:32

问题


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.

My code is the following:

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

int main(){
    void *(intr_handlerptr)(int);
    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?


回答1:


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);

and then your code compiles properly

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

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

void *(intr_handlerptr)(int);

is the same as:

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)



来源:https://stackoverflow.com/questions/53349751/lvalue-required-as-left-operand-of-assignment-for-function-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!