12

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 function that accept variable number of arguments. like

void function (int paramN, ...);

In my program, there are no way to know, which is the type of variable arguments, it can be a char, an array, an int, an short, an function point, etc... like

function (paramN, "Hey, I'm a string", 1, function_pint, array, -1); // -1 is a sentinel.

So, I think that, an int, is 32bits, in a x86(32bits) system, this will hold all address of memory. So, if I get all arguments with a int, it will not be a problem, for example, "Hey, I'm a string" the address of this string, fit normally in a 32bits variable, so, i need only to make a cast.

Im right?
Can I do it?
Note: I don't want to make my function like printf (this solution, don't fit in this case ok?)

Thanks for all answer.
And sorry for my bad english.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
drigoSkalWalker
  • 2,742
  • 9
  • 32
  • 45

6 Answers6

14

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.

jldupont
  • 93,734
  • 56
  • 203
  • 318
  • Can you explain more better this? I think that I understood, for example, I can make a enum with the possible types { INT, SHORT, CHAR, ARRAY...ETC }, than, the i will make a struct with two fields, one field have the type of enum right? and the other one have the value ok? its correct? how to hold the value? sorry if i'm wrong and thanks for your answer. – drigoSkalWalker Nov 07 '09 at 00:41
12

You can't do it the way you describe it.

The C calling convention is for the caller to put arguments on the stack but it does not put any information on types, so the callee must have a way to find it (at least size of variables).

  • No problem for functions with prototypes every type is known.

  • With functions with variable number or parameters (variadic) it's more tricky, you have to call va_arg for each argument to read each variable and you must provide the type for va_arg. If you provide a type that is not the real one the compiler won't complain (it can't it's runtime information) but anything can happen (usually something bad).

Hence you have to pass the type.

In some cases you can predict type (eg: a counter followed by some ints, and so on), but usually you pass it encoded in parameters. You can pass it encoded in a format string like printf does, the union trick described by jldupont is also common enough.

But anyway you have to pass it.

You really can't rely on underlying data binary representation, not even data size. Even if the program seems to work when you write it, it has no compatibility and can break with any change of the system, of the compiler, or even when changing compiling options.

Let's go with an example where you pass the type followed by the argument (thus neither the union trick nor the format string like printf). What it does is converting all it's passed value to double and add them, no really useful isn't it:

#include <stdio.h>
#include <stdarg.h>

enum mytypes {LONG, INT, FLOAT, DOUBLE };

double myfunc(int count, ...){
    long tmp_l;
    int tmp_i;
    double tmp_d;
    double res = 0;
    int i;

    va_list ap;
    va_start(ap, count);
    for(i=0 ; i < count; i++){
        int type = va_arg(ap, enum mytypes);
        switch (type){
            case LONG:
            tmp_l = va_arg(ap, long);
            res += tmp_l;
            break;
            case INT:
            tmp_i = va_arg(ap, int);
            res += tmp_i;
            break;
            case FLOAT:
            /* float is automatically promoted to double when passed to va_arg */
            case DOUBLE:
            tmp_d = va_arg(ap, double);
            res += tmp_d;
            break;
            default: /* unknown type */
            break;
        }
    }
    va_end(ap);
    return res;
}

int main(){
    double res;
    res = myfunc(5,
        LONG, (long)1,
        INT, (int)10,
        DOUBLE, (double)2.5,
        DOUBLE, (double)0.1,
        FLOAT, (float)0.3);
    printf("res = %f\n", res);
}

This exemple use the new stdarg variadic header defined in C99. To use it you need to have at least one fixed parameter to your function (in this exemple it's count). The good thing if that thus you can have several variadic lists in your function (ie something like myfunc(int count1, ..., int count2, ...)). The bad thing is that you can't have a purely variadic function (ie something like myfunc(...) like with the old format. You can still use the old format using varargs compatibility headers. But it is more complicated and rarely necessary, because you need types but also some way to know the list is finished and something like count is handy (but not the only way to do it, a 'terminator' could be used for instance).

kriss
  • 23,497
  • 17
  • 97
  • 116
3

You can't do this with variable arguments the way you're describing because no information about the type of the arguments you pass is retained after compilation unless you do it explicitly. Even passing the address of the argument variable won't tell you this, because the bits in memory that represent a string could represent a number or something else.

To make varargs work with variable types, you can store type information in the arguments themselves (e.g., as described by jldupont in his answer), or you can store the information in a non-variable argument (e.g., a format string like printf's).

Nathan Kitchen
  • 4,799
  • 1
  • 24
  • 19
2

To expand on kriss idea

Since C99, use _Generic to pass to the variadic function pairs of arguments.

Change the call to use a macro G(obj)

// test("Hello", "world", 15, 16.000);
test(G("Hello"), G("world"), G(15), G(16.000), 0);

G is a macro the uses _Generic to distinguish types and provide an enumerator. Then 2 arguments to test are passed: an enumerator to ID the type and then the object itself.

This obliges _Generic to enumerate many types. There are infinite numbers of possible types, but a basic list _Bool, char, long long, double, char *, etc. is reasonable.

Then test() uses the enumeration to steer selection of the next argument. Recommend a 0 to end the list.


A more complete example:
Formatted print without the need to specify type matching specifiers using _Generic

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • I checked your ways here https://github.com/proxy-m/bmp_on_linux/blob/master/strcats.h without _Generic, with help of c99, gcc __typeof__, gcc __builtin_types_compatible_p . (See also https://stackoverflow.com/questions/4421681/how-to-count-the-number-of-arguments-passed-to-a-function-that-accepts-a-variabl/35693080#35693080 ) – user1742529 Jun 03 '21 at 16:03
1

Coping from http://en.wikipedia.org/wiki/Stdarg.h:

There is no mechanism defined for determining the [...] types of the unnamed arguments passed to the function. The function is simply required to know or determine this somehow, the means of which vary.

That is your function can't know just from the arguments which arguments are strings. You will need to tell your function what to expect somehow. That is why the printf convention is so common.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • Note: I don't want to make my function like printf (this solution, don't fit in this case ok?) – drigoSkalWalker Nov 07 '09 at 00:24
  • 2
    We understood that. There is *no* easy way to accomplish what you've asked for. You *must* find a way to tell a varags function what kind of data is in the arguments. The `printf` convention isn't the only choice (see the link or jldupont's answer), but you always have to say. – dmckee --- ex-moderator kitten Nov 07 '09 at 01:18
  • Ok dmckee, thanks for your answer, i only want to get other way to accomplish this ok? anyway, thanks for your answer guy! ;D – drigoSkalWalker Nov 07 '09 at 12:42
0

Try u?intptr_t, which is defined in stdint.h. This is an integer which is guaranteed to be big enough to hold a pointer.

You might also want to think about what happens when you pass a floating point number; it is converted to and passed as a double. If your program is expecting an int this will destroy its view of the stack. Ouch. And the compiler cannot catch this.

And your function, as defined, has (barring any bit-encoding in paramN, in which case it should be an enum or bitfield, or, at least, unsigned) no way of knowing what the type of parameters it receives are, and as such is unlikely to be able to do anything useful with them. C has no run-time type information on its objects.

What, exactly, are you trying to do?

Tim Schaeffer
  • 2,616
  • 1
  • 16
  • 20
  • Hi man, your answer is great, i'm trying to pass some function points to my function of variable arguments, but between ones, i will pass a string or a int too, i think that i have no problem to use int to accomplish this, or, to use intptr_t too, what you tell me about it? thanks for your answer o/ – drigoSkalWalker Nov 07 '09 at 00:28
  • Your program has to know what the type of each argument is, and the only way to to know this in your function is to somehow pass that information through your arguments. If you call printf like printf("%s:%d","string",i) it uses its first argument as a string and second as an int because the format string tells it to do that. If the user passes something else, like printf("%s:%d",0,"x") printf can't know that, and your program is in error. How are you telling your functions how to use its arguments? How does it know which ones are ints, which are strings, which are pointers, &c? – Tim Schaeffer Nov 07 '09 at 21:10