#Better way of handling a variable number of arguments

20 messages · Page 1 of 1 (latest)

worldly fractal
#

Does anyone know of a better way to handle a variable number of arguments other than variadic functions? Like unions or something?

uncut pilotBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

limber zephyr
#

Whats your use case? I'm curious

worldly fractal
#

basically I'm trying to have a function to send an escape code to the terminal ERROR tsend_escape(escape_t escape, escape_args_t args); I was just going to go with having a union, but then you have to create an entirely separate variable because you can't directly pass unions to function.

hard coral
#

I’m still confused why you need variadic arguments

#

Do you want to pass any number of escape_t objects?

worldly fractal
#

escape_t is an enum

hard coral
#

And you want to pass any number of that type of object?

#

Or is each possible value of escape_t associated with a certain number of arguments of different types?

worldly fractal
#

yes

hard coral
#

Yes to which one of those

worldly fractal
#

the second

#

this is what it looks like: ```
typedef enum {
ESCAPE_smcup,
ESCAPE_rmcup,
...
ESCAPE_set_fgcolor
ESCAPE_set_cursor_pos,
} escape_t;

typedef union {
uint8_t set_fgcolor;
uint16_t set_cursor_pos[2];
} escape_args_t;

the first 2 require no arguments, so you just you'd have to do ```
escape_args_t args = {0}; 
tsend_escape(smcup, args)

which is a rather unapealing api. also to pass any arguments you'd also have to create a second variable which is just annoying if you want to just pass it in directly

hard coral
#

Then you could do something like

union escape_args {
     struct { } smcup_args;
     struct { } rmcup_args;
     struct { uint16_t cursor_pos[2]; } set_cursor_args;
};```
worldly fractal
#

would I not still have to create the additional variable on the side?

hard coral
#

I believe you would be able to create it inline. I forget the exact syntax for it, since I don’t use it

#

I would guess it’s something like (union escape_args){ .smcup_args = {} }

worldly fractal
#

ah yes, thank you

uncut pilotBOT
#

@worldly fractal Has your question been resolved? If so, type !solved :)

worldly fractal
#

!solved