Search code examples
cpointersstructfunction-pointers

C, pointer to function in struct, warning "assignment from incompatible pointer type"


I have a struct defined as follow:

typedef struct {
    char (*behave) (int*);
} BEHAVIOURSTRUCT;

this struct is defined in a .h file and it is included in the .c file There i have a global variable (MAX_BEHAVIOURS is defined as 3):

BEHAVIOURSTRUCT bhvr[MAX_BEHAVIOURS];

and in the init i try to assign this, but here i get the warning: "assignment from incompatible pointer type"

void init() {
bhvr[0].behave = BHVR_line_follow; // here
...
}

the function i am trying to asign

void BHVR_line_follow(int *currstate){
....
}

by the sound of it my declaration in the struct and the pointer to the function are not from the same build, but in my opinion they are. But most likely i am mistaken.


Solution

  • Here is your function, and the function-type required, side-by-side:

    void BHVR_line_follow(int *currstate)
    char (*behave) (int*)
    

    The function-type required takes an int* and returns a char.
    Your function takes an int* and returns void (nothing).

    To summarize, the return-type of BHVR_line_follow is wrong.