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: 30

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,826
Website

Re: need clarifications in callback structures

1)  Why function pointer "fp" passes reference of struct "node" itself ?

In the case of the given node struct here, I have no idea, since it doesn't seem to make much sense...  But, given a struct with more data in it, it might want access to that other data for some reason...  But, yeah, there's absolutely no reason to pass it in the given example, since there's nothing in there it doesn't already have access to via other means...

2)  Who is passing values to "data" parameter to function "my_node" here ? (see arrow in code)

Well, main() is doing so here:

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

3)  It seems there is a duplicate info. printed by function "my_node". Is this correct ?

Yeah...  Like I said, that's why passing the "struct node" to the function makes no sense here...  The "data" is already passed separately, and the only other thing inside "struct node" is the function pointer itself, so it's just utterly pointless to pass it to the function...  (Nevermind the fact that in this particular example the struct is a global variable which the function could just directly access anyway if it wanted to!)

4)  Is this correct way of defining and calling callback structures?

*shrug*  There's no one true way to do much of anything...  Given this example, I'd find it very odd for the reasons mentioned already...  But, it's not necessarily "incorrect"...  It's just not how *I* personally would do things...  But, the example is rather contrived and not really indicative of real-world use, either...  In general, the callback function is going to be called by some library code when some condition occurs, and not just arbitrarily by main() like that...  Depending on what exactly that library code is intended to do and what that condition is exactly, the nature of the callback and its args will vary...  But, in general, there will be an argument to let the callback know what condition occurred, and any other relavent details about it...  And, yes, the arbitrary user-supplied "data" arg is standard, just so that users may pass onto their callback function any arbitrary data they wish for any necessary use they need...

Offline

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

Board footer

Powered by FluxBB