GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Monday, May 30, 2011

Below is a simple example of function pointer as a member of struct:

#include <stdio.h>

struct data{
    void(*hello)();
};

void hello(){
    puts("Hello comes from C");
}

int main(){
    // the first way
    struct data d;
    d.hello = hello;
    d.hello();

    // the alternate way
    struct data dd;
    struct data* ptr = &dd;
    ptr->hello = *hello;
    ptr->hello();
  
    return 0;
}

Share/Bookmark

No comments:

Post a Comment