2016年5月6日 星期五

[C] Write function in C structure

#include <stdio.h>

typedef struct hello {
    int (*someFunction)(int parameter);
} hello;

int foo(int parameter) {
    return parameter;
}

hello Hello() {
    struct hello aHello;
    aHello.someFunction = &foo;
    return aHello;
}

int main()
{
    struct hello aHello = Hello();
    printf("Print hello: %d\n", aHello.someFunction(3));

    return 0;
}

Result :
Print hello: 3

Reference:

0 意見:

張貼留言