简体   繁体   中英

create a button structure in C

I am trying to create a structure called button ressource, that will store all the attributes of a button.

My problem is with the command function "void on select" which should be a function with unkonow number and type of parameters right now i'm using a pointer on a function that takes only one param that i've declared as one of the structure element, but i need a better way in case i need to execute a command with unkonw number and type of parameters.

struct button_ressource 
{
    unsigned char *image_off;
    unsigned char *image_on;
    unsigned int width
    unsigned int h;
    void (*on_select)(void *data);  // the button command
    int param;  // this param will be passed to the on_select command
};
typedef struct button_ressource button_res; 

You can't do that, in C.

You're going to have to figure out a common "protocol" for all callbacks, or use a union of a bunch of different ones (plus add information so you know which one to use) or something like that.

You can use variable arguments, but then you have to use an explicit va_list argument, you can't "pass on" the ... part directly.

In you solution, even if we assume you could express the function itself, how would you deal with the parameters? You can't replace int param; with anything param[ANY_AMOUNT]; , there is no anything in C and an array can't have an infinite length either ... I'm trying to illustrate that your "dream solution" doesn't work in C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM