jweinst1
3/26/2017 - 8:04 PM

func pointer on struct

func pointer on struct

#include <stdio.h>
#include <stdlib.h>


//c puts function pointer on struct

int addInt(int n, int m) {
    return n+m;
}

struct lib {
    int (*func)(int,int);
};



int main() {
    struct lib a;
    //attaches function pointer to struct
    a.func = &addInt;
    int sum = (*a.func)(2, 3);
    printf("The sum is %d", sum);
    //The sum is 5
    return 0;
}