#Better way of handling a variable number of arguments
20 messages · Page 1 of 1 (latest)
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.
Whats your use case? I'm curious
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.
I’m still confused why you need variadic arguments
Do you want to pass any number of escape_t objects?
escape_t is an enum
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?
yes
Yes to which one of those
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
Then you could do something like
union escape_args {
struct { } smcup_args;
struct { } rmcup_args;
struct { uint16_t cursor_pos[2]; } set_cursor_args;
};```
would I not still have to create the additional variable on the side?
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 = {} }
ah yes, thank you
@worldly fractal Has your question been resolved? If so, type !solved :)
!solved