0

I need to write a C code using libevent. This is briefly what I expect to do. From stdin, a character will go through in_read_cb. in_read_cb will sent to master_read_cb. master_read_cb will then send to the system to process. These 2 function are set by libevent. So the type of variable passes to them are fix.

void in_read_cb(struct bufferevent *bev, void *data)
{
//do something
}

void master_read_cb(struct bufferevent *bev, void *data)
{
//do something
}

The problem is, I need to put that void *data into a structure, while adding a new variable as identifier for the master_read_cb when to start processing. It look something like this:

typedef struct _new_data
{
void *data;
int start;
} new_data;

void in_read_cb(struct bufferevent *bev, struct new_data->data)
{
//do something
//when it meets a condition, new_data->start = 1;
}

void master_read_cb(struct bufferevent *bev, struct new_data->data)
{
//check new_Data->start
//will sent the character for processing if start = 1
}

This is totally wrong because of how I pass in the function. How can I do it the correct way?

Mohd Fikrie
  • 197
  • 4
  • 21
  • Just pass the `data` member from the structure? E.g. `struct _new_data data; in_read_cb(you_bev, data.data);` – Some programmer dude Jan 02 '14 at 08:41
  • You pass a `void**` if you need to change the structure's `data` member, i.e., `master_read_cb(bev, &your_struct_instance->data)`. If you need only change the value it *points* to then just pass the `data` pointer itself. – Ed S. Jan 02 '14 at 08:41
  • 1
    Oh and by the way, don't have global names with leading underscores, those are reserved by the specification. – Some programmer dude Jan 02 '14 at 08:41
  • @JoachimPileborg ok. thx for telling me. I was just making example. I need to pass both of the structure's member since master_read_cb will need to check either start is 1 or 0. – Mohd Fikrie Jan 02 '14 at 09:18

1 Answers1

0

You'll need something like this and pass a pointer to struct _new_data into them:

void in_read_cb(struct bufferevent *bev, void *new_data_ptr)
{
    new_data *ndata = new_data_ptr;
    //do something
    //when it meets a condition, ndata->start = 1;
}

void master_read_cb(struct bufferevent *bev, void *new_data_ptr)
{
    new_data *ndata = new_data_ptr;
    //check ndata->start
    //will sent the character for processing if start = 1
}
starrify
  • 14,307
  • 5
  • 33
  • 50