General discussion

Locked

variable number of args to functions

By saratchandra_a ·
Hai, I wish to know about how to pass variable number of parameters to functions in C language. I know about va_list,va_arg,va_start but va_arg doesn't support either char or float data types I want to handle these too. So please help me out of this.

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

variable number of args to functions

by wjhunt In reply to variable number of args ...

I would probably send the same amount of parameters everytime. If the parameter is not relevant you could assign it a dummy value. Then in your function you could by pass these parameters due to the dummy value...

function myfunc(a,b,c,d)
{if (a != '999')...
}

Collapse -

variable number of args to functions

by saratchandra_a In reply to variable number of args ...

The question was auto-closed by TechRepublic

Collapse -

variable number of args to functions

by Jay Eckles In reply to variable number of args ...

make the argument to the function an array; the array could then be of arbitrary length. Each item in the array would be an argument. The only downside to this is if you want to have different types of arguments (e.g. one int, one char, one float). If that is a problem, then I'd have multiple arrays as parameters - first of type int, second of type x, third of type y, etc. exhausting the types of parameters that could be passed.

Good luck.

Jay Eckles

Collapse -

variable number of args to functions

by saratchandra_a In reply to variable number of args ...

The question was auto-closed by TechRepublic

Collapse -

variable number of args to functions

by Wayne M. In reply to variable number of args ...

I would probably take a closer look at your implementation of va_arg to see if maybe it does support char and float. If you can do a printf() for these data types, va_arg probably supports them.

If va_arg really does not support them, then try accessing the data as a different type and casting it back to the real type. Depending on how your processor pushes things on the stack, this may require some experimentation, but I would guess that a char might be passed as an int and a float as a long or double long. For the float, you may have to pull two longs and cast either the first or second to get the correct value.

I hope this provides enough hints that you can figure out the problem.

Collapse -

variable number of args to functions

by saratchandra_a In reply to variable number of args ...

The question was auto-closed by TechRepublic

Collapse -

variable number of args to functions

by saratchandra_a In reply to variable number of args ...

This question was auto closed due to inactivity

Back to Web Development Forum
7 total posts (Page 1 of 1)  

Related Discussions

Software Forums