Variable arguments in C, how to get values with a generic type?

前端 未结 6 1632
花落未央
花落未央 2020-12-01 21:00

I\'m trying to use C stdarg.h lib, with a generic type. The type int, is my generic type > to understand it, please, hold reading. So, my problem is:

I have a functi

6条回答
  •  旧巷少年郎
    2020-12-01 21:54

    Use a void * (or a typed structure) for each parameter & use a structure with a "type" argument (an integer). A pointer / union to contain the actual value.

    In other words, each parameter is passed with a pointer to a typed structure. Each instance of this typed structure contains a value. The type of this "value" is contained in this typed structure.

    Better Example:

    typedef struct  {
      int type;
      union {
        int int_value;
        double double_value;
        ...
      };
    } Param;
    

    void function(Param *p1, Param *p2, ...)

    The latest example of such trick I ran into was DBus.

提交回复
热议问题