Stubborn
Stubborn

Reputation: 968

lvalue required as left operand of assignment for function pointers

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?

Upvotes: 3

Views: 358

Answers (1)

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140186

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)

Upvotes: 2

Related Questions