UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » need clarifications in callback structures

#1 2012-08-22 08:31 AM

useless79
Member
Registered: 2011-10-10
Posts: 32

need clarifications in callback structures

Hi All,

Though I have seen these types of coding with callback structures several times but still few things are beyond my intuition.
Can some body explain me few points here?

1)  Why function pointer "fp" passes reference of struct "node" itself ?
2)  Who is passing values to "data" parameter to function "my_node" here ? (see arrow in code)
3)  It seems there is a duplicate info. printed by function "my_node". Is this correct ?
4)  Is this correct way of defining and calling callback structures?


struct node
{
    void (*fp)(struct node*, void*);
    void  *data;
};

struct node saved = { 0, 0 };

void node_register(void (*fp)(struct node*, void*), void *user_data)
{
    saved.fp   = fp;
    saved.data = user_data;
}

void my_node(struct node *nd, void *data)  ==============> Who is passing values to "data" parameter here ?
{
    printf("Inside function... %s\n", __func__);
    printf("data1: %s\n", (char *)data);                
    printf("data2: %s\n", (char *)nd->data);
}

main(void)
{
    node_register(my_node, "Hello");
    saved.fp(&saved, saved.data);
}

Thanks.....................

Offline

#2 2012-08-22 12:44 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: need clarifications in callback structures

saved.fp(&saved, saved.data);

Offline

  • Index
  • » C
  • » need clarifications in callback structures

Board footer

Powered by FluxBB