What good is a function pointer inside a struct in c?

11,881

Solution 1

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;
}

Solution 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.

Solution 3

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

Share:
11,881
Nithish Inpursuit Ofhappiness
Author by

Nithish Inpursuit Ofhappiness

SOreadytohelp

Updated on June 09, 2022

Comments

  • Nithish Inpursuit Ofhappiness
    Nithish Inpursuit Ofhappiness almost 2 years

    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?