Touk
Touk

Reputation: 479

How to know if my parameter is a char or a char* with varargs

I have a function who takes a two parameters, the fist one is always a char* the second one can be a char or a char*

then, this is what i want to do

void   my_function(char  *arg1, XXXX arg2)
{
 if (XXXX == char)
        //convert arg2 in char*
  do_something_else(arg2);
}

the problem is, i dont know how i can get the type of arg2

Upvotes: 0

Views: 85

Answers (1)

2501
2501

Reputation: 25753

You can pass a struct containing your types with an additional member that selects the chosen type.

struct my
{
    char* s ;
    char c ;
    int type ;
}

And then use a check that does the right thing:

if( m.type == 1 )
{
   m.s = ... //do something with char*
}
else if( m.type == 2 )
{
   m.c = ... //do something with char
}

If you insist on using a variable arguments function, then use the approach used by printf(). The first argument contains number and types of every passed optional arguments.

Upvotes: 1

Related Questions