6

I guess using a function pointer inside a struct has something to do with encapsulating a function within a structure...? If so, then how exactly is this achieved??

And what good does it bring to have a function pointer inside a struct rather than simply defining the function?

  • 1
    The advantage is that each variable of the struct type can have its own function. That way, you can you have a polymorphism-like mechanism. – Fabien Mar 25 '13 at 10:21
  • This is something like constructors/destructors from C++. Also by this way you can make inheritance. – Eddy_Em Mar 25 '13 at 10:22
  • 2
    It is all hollow talk without concrete, practical examples of code. Show some code you want to understand. – n. m. could be an AI Mar 25 '13 at 10:28
  • This is not exactly an object in the OOP sense since the method (i.e. the function pointer in the structure) can be different for each instance of the structure. You can see _GObject_ tools for a deeper approach on that subject : https://developer.gnome.org/gobject/stable/howto-gobject.html – Rerito Mar 25 '13 at 10:29
  • read [How pointers to function as struct member useful in C?](http://stackoverflow.com/questions/12971995/how-pointers-to-function-as-struct-member-useful-in-c) – Grijesh Chauhan Aug 23 '13 at 07:49

4 Answers4

6

function pointers inside structures are the base of object-programming in C (see http://www.planetpdf.com/codecuts/pdfs/ooc.pdf ). It is really really for medium-to-large C projects.

An example:

Header:

typedef struct TPile
{
    int(*Push)(struct TPile*, int);
    int(*Pop)(struct TPile*);
    void(*Clear)(struct TPile*);
    void(*Free)(struct TPile*);
    int(*Length)(struct TPile*);
    void(*View)(struct TPile*);

    int Nombre;

    struct Titem *Top;

} TPile ;

Source:

TPile TPile_Create()
{
    TPile This;
    TPile_Init(&This);
    This.Free = TPile_Free;

    return This;
}


TPile* New_TPile()
{
    TPile *This = malloc(sizeof(TPile));
    if(!This) return NULL;
    TPile_Init(This);
    This->Free = TPile_New_Free;

    return This;
}


void TPile_Clear(TPile *This)
{
    Titem *tmp;

    while(This->Top)

    {
      tmp = This->Top->prec;
      free(This->Top);
      This->Top = tmp;
    }

    This->Nombre = 0;
}
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
lucasg
  • 10,734
  • 4
  • 35
  • 57
2

Having function pointer inside struct will be useful for certain data structures such as binary search tree.

Lets say , i want to insert an element whose struct is

struct Employee {
      int eid;
      char *name;
 };

into a binary search tree. but i would like the BST to use my function to compare the elements while storing and searching.

and the bst struct will be as follows.

 struct BST {
     struct _node *root;
     int (*compare)(void *e1 , void *e2);
 };

Now, i will use the BST as follows.

  int main(void){
      struct Emp e1  = { 1, "John" };
      struct BST *t = create_tree();
      t->set_compare( &compare );

      t->insert(e1);


      t->get(e1);
       ...
  }

  int compare(void *e1 , void *e2)
  {
      //type cast e1, e2 as struct Emp 
      // return the comparison result based on id
  } 

The advantage i see is i don't need to keep on pass this function pointer into my all BST operation functions .

but storing all public functions inside struct will bring the OOP style inside C code , like what others says.

nvseenu
  • 125
  • 6
1

Assume the function takes 2 variables and calls 4 different functions. Let there be a structure as follows:

/* Input 1                Input 2                 Function pointer
{
{ 0,                       0,                   Function00},
{ 0,                       1,                   Function01},
{ 1,                       0,                   Function10},
{ 1,                       1,                   Function11}
}

It would be easy to compare the input values against the structure values and call corresponding function.

It might seem it is better to use if..else... But think of cases in which there are more than 100 such cases to be checked

hazzelnuttie
  • 1,403
  • 1
  • 12
  • 22
0

the benifit of having function pointer defined in the structure, is related to the code design. the goal is to get your code more structured.

Defining variables and functions in a structure is like defining class in oriented object languages

Refer to the following link for more details

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268