I have this function:
void func(int a, int b, char s, FILE *fp, int flag)
and i want to use the function's args based on the flag. For example:
func(num, NOTHING, NOTHING, file, 1)func(num, othernum, NOTHING, NOTHING, 2)and on the function this_is_function i want to have:
void func(int a, int b, char s, FILE *fp, int flag){
if(flag == 1){
/* use a and file */
}
if(flag == 2){
/* use a,b */
}
/* etc etc */
}
i would like to know if it is possible and how to do this!Thanks in advance :)
If by NOTHING you mean that you really want to omit that argument, I don't think it's possible the way you've outlined it.
The way this typically is done in C is through variable argument lists. That would mean you'd have to reformulate so that the flag goes first, since it decides the rest of the arguments:
void func(int flag, ...)
{
va_list args;
int num, othernum;
FILE *file;
va_start(args, flag);
if(flag == 1)
{
num = va_arg(args, int);
file = va_arg(args, FILE *);
}
else if(flag == 2)
{
num = va_arg(args, int);
othernum = va_args(args, int);
}
va_end(args);
/* Inspect `flag` again, and do things with the values we got. */
}
Then you can use the function like so:
func(1, 42, a_file);
or
func(2, 17, 4711);
This of course requires lots of care, since you're not getting a lot of help from the compiler anymore to match the values provided in the call to what the function expects.
I would recommend restructuring it into different top-level functions instead, that call a common "worker" function with the proper arguments:
func_mode1(42, a_file);
func_mode2(17, 4711);
these could then call func() with the proper flag value, filling in suitable defaults for arguments that don't apply (such as NULL for a non-used file pointer).
You can pass 0 to int and char and NULL for pointers when you call the function and the arguments are not used.
func(num, 0, 0, file, 1)
func(num, othernum, 0, NULL, 2)
You can also use variadic functions. Variadic functions are functions with a variable number of arguments.
Create seperate functions for each of the cases you are trying to handle. This would yield to more readable code. State your intention through your code rather than hack around it.
Your code should work fine and moreover try it out once and check for any errors. I am sure as no error should occur. You can also try using switch case in the func with flag as the choice for switch. I am sure you are not going to use Nothing and othersum in your code.
Related
I have a function that accepts 3 parameters, however I also want it to only accept the first two and not care about the third. How can I do this in C. I tried just declaring the function in the header file as void list()
thinking this might imply that we don't care how many parameters but this didn't work.
void list(uint8_t _pin, unsigned int time, unsigned long tasks)
What you want is called Variadic function. It can accept variable number of arguments.
**Famous examples: printf()/scanf()
These functions contain an ellipsis (…) notation in the argument list, and uses special macros to access the variable arguments.
The most basic idea is to write a function that accepts a variable number of arguments. So, it must be declared with a prototype that says so.
The syntax of ISO C requires at least one fixed argument before the …. thus, we write the fixed arguments as usual, and then add the … notain at the end of the parameter list to indicate the possibility of additional arguments. For example,
int func (const char *a, int b, …)
{
…
}
defines a function func() which returns an int and takes two required arguments, a const char * and an int. These are followed by any number of anonymous arguments.
You can check the On-line GNU manual for more details.
Here is a trick explained in SO here : C default arguments.
You can make third parameter as default in function declaration as follows:
typedef struct {
uint8_t _pin;
unsigned int time;
unsigned long tasks;
} f_args;
double var_f(f_args in){
uint8_t _pin = in._pin? in._pin : 8;
unsigned int time = in.time ? in.time : 3;
unsigned long tasks =in.tasks? in.taska : 0;
return f_base(i_out, x_out);
return list(_pin, time, tasks);
}
#define list(...) var_f((f_args){__VA_ARGS__});
So you can call your function as follows:
list(2,5);
A different solution to your problem without variable parameters is to do something like this:
void list(int argc, char **argv);
You have two parameters always, however what changes is the contents of argv and the count of what is inside it. You might have seen this in the entry point (main function) of other C programs. The parameters argc, argument count, and argv, argument vector, give the number and values of what you need.
Then inside list you could do something like:
if (argc == 2)
do_something;
else if(argc == 3)
do_something_else;
else
whatever;
I am in a problem with a design question in C.
Let's say that I have a pretty large amount of functions, with different argument count.
POQ:
int print_one(int x)
{
printf("one: %d\n", x);
return 1;
}
int print_three(int x, int y, int z)
{
printf("three: %d-%d-%d\n", x, y, z);
return 3;
}
Now, I want to connect some properties to these functions in a structure, so that I can manipulate them without knowing the exact function, including their parameter count (I might even call the structure interface)
I tryd it like this, (& I think is pretty wrong):
typedef int (*pfunc)(int c, ...);
typedef struct _stroffunc
{
pfunc myfunction;
int flags;
int some_thing_count;
int arguments[10];
int argumentcount;
} stroffunc;
int main()
{
stroffunc firststruct;
firststruct.pfunc = (pfunc) print_two;
firststruct.something_count = 101;
arguments[0] = 102;
argumentcount = 1;
flag &= SOME_SEXY_FLAG;
// now I can call it, in a pretty ugly way ... however I want (with patially random results ofc)
firststruct.pfunc(firststruct.arguments[0]);
firststruct.pfunc(firststruct.arguments[0], 124, 11);
firststruct.pfunc(1, firststruct.arguments[0], 124, 1, 1);
}
I find this solution very ugly, & I think (hope) that there is a better solution for calling & and setting the function pointers.
I'm just hoping, that I was clear enough ...
NOTE: I didn't compile this code, but i compiled & run a very similar one so the concepts are working.
NOTE: pure C needed
Calling a non-variadic function through a variadic function pointer leads to undefined behaviour. For a start, recall that the arguments to variadic functions undergo the default argument promotions (chars are converted to ints, etc.), which will totally screw things up.
It's not clear how or why you intend to dynamically call a function with differing numbers of arguments. But one solution could be to use a union:
typedef struct {
int num_args;
union {
void (*f1)(int);
void (*f2)(int, int);
void (*f3)(int, int, int);
} func;
} magic;
...
magic m;
...
switch (m.num_args) {
case 1: m.func.f1(arg1); break;
case 2: m.func.f2(arg1, arg2); break;
case 3: m.func.f3(arg1, arg2, arg3); break;
default: assert(0);
}
A second solution would be to rewrite all of your functions as variadic.
This is on the edge of my knowledge, but I believe that you need to make the functions variadic as well, due to incompatibilities in the ABI.
see: wikipedia's example
Maybe you can add a library which have some functions to handle the struct, like a "pseudo" class,
int initFunc(int (*pfunc)(int c,...));
This function will save the pointer into the struct, like a context in POO, in the struct you will use it like a "map" of all the function an you will call each one using an id.
which returns an id, and you save it in an array,
then another func say
int call(int id,int p1,...);
where you say the function id and the parameters, sure you must now which function is each id
I have a 3rd party function with signature:
int secretfoo(int numargs, ...);
I can call it directly, but what I really want is wrap it with my function that adds some extra arguments to it.
Assume simple case of integers: I want calls secretfoo(2, 10, 20) to be translated as this: when I see argument 10 to duplicate it and make the call: secretfoo(3, 10, 10, 20). I want to do it in wrapper:
int foowrapper(int numargs, ...);
This wrapper analyze argumetns and call secretfoo as described above.
Can this be done in portably with va_list / va_arg etc.? Any other way?
There is no portable way to manipulate the arguments in a variable argument list directly, because it is highly platform dependent how such arguments are passed into the function. And on most hardware architectures, there is absolutely no way to insert additional arguments in the middle or the end of the list.
If there is a practical upper limit to the number of arguments, then it could be done by extracting all the arguments to foowrapper and 'manually' building the new argument list for the call to secretfoo.
The code would look something like this:
int foowrapper(int numarg, ...)
{
va_list args
int newargs[numarg*2]; /* worst case allocation */
int numnewargs = 0;
/* Extract the arguments */
va_start(numarg, args);
for (int i=0; i<numarg; i++)
{
newargs[numnewargs++] = va_arg(args, int);
/* duplicate value 10 as you encounter it */
if (newargs[numnewargs-1] == 10)
{
newargs[numnewargs++] = 10;
}
}
/* Forward to the secretfoo function */
switch (numnewargs)
{
case 0: return secretfoo(0);
case 1: return secretfoo(1, newargs[0]);
case 2: return secretfoo(2, newargs[0], newargs[1]);
/* etc... */
}
}
I'm afraid it can't be done portably. stdarg.h "defines four macros" (latest C standard draft): va_start, va_end, va_arg and va_copy. None of these can be used to convert a va_list back to a variable number of values, other than one-by-one.
Your third party library should have supplied a function vsecretfoo(int, va_list), like the standard library does for these cases (vprintf, etc.).
Converting a C++ lib to ANSI C and it seems like though ANSI C doesn't support default values for function variables or am I mistaken?
What I want is something like
int funcName(int foo, bar* = NULL);
Also, is function overloading possible in ANSI C?
Would need
const char* foo_property(foo_t* /* this */, int /* property_number*/);
const char* foo_property(foo_t* /* this */, const char* /* key */, int /* iter */);
Could of course just name them differently but being used to C++ I kinda used to function overloading.
No, Standard C does not support either. Why do you feel you need to convert your C++ code to C? That could get quite tricky - I'd have thought writing wrappers would be the way to go, if your C++ must be callable from C.
Nevertheless I found a "trick" to do so if you use GCC (edit December 2020) or any compiler compatible with C++2a -yes it works with 'plain C' since it is a pre-compiler trick-.
GCC has a handy ## extension on variadic macro that allows you to simulate a default argument.
The trick has limitations: it works only for 1 default value, and the argument must be the last of you function parameters.
Here is a working example.
#include <stdio.h>
#define SUM(a,...) sum( a, (5, ##__VA_ARGS__) )
int sum (a, b)
int a;
int b;
{
return a + b;
}
main()
{
printf("%d\n", SUM( 3, 7 ) );
printf("%d\n", SUM( 3 ) );
}
In this case, I define SUM as a call to sum with the default second argument being 5.
If you call with 2 arguments (first call in main), it would be prepocessed as:
sum( 3, (5, 7) );
This means:
1st argument is 3
second argument is the result of the sequence (5, 7)... which is
obviously 7!
As gcc is clever, this has no effect on runtime as the first member of the sequence is a constant and it is not needed, it will simply be discarded at compile time.
If you call with only one argument, the gcc extension will remove the VA_ARGS AND the leading coma. So it is preprocessed as:
sum( 3, (5 ) );
Thus the program gives the expected output:
10
8
So, this does perfectly simulate (with the usual macro limitations) a function with 2 arguments, the last one being optional with a default value applied if not provided.
Edit
-a) It does also work with CLANG (and possibly other compilers)
-b) A version that does NOT complain about unused arguments:
#define DEF_OR_ARG(z,a,arg,...) arg
#define SUM(a,...) sum( a, DEF_OR_ARG(,##__VA_ARGS__,__VA_ARGS__,5))
[Edit - October 2020] :
You could also try the new __VA_OPT__ that was standardised with c++2a (and should work in plain C too) instead of ## which is a gcc extension. Typical usage is __VA_OPT__(,) that would add the coma when the argument list is non-empty and otherwise outputs nothing.
[Edit - December 2020] :
So the above trick, with __VA_OPT__, becomes:
#define DEF_OR_ARG(value,...) value
#define SUM(a,...) sum( a, DEF_OR_ARG(__VA_ARGS__ __VA_OPT__(,) 5))
Unlike the 'sequence trick' that might complain for unused variables, this only involves the pre-compiler and is more readable.
When SUM is called with only one argument, ... is empty and __VA_OPT__ does not output anything, thus DEF_OR_ARG(__VA_ARGS__ __VA_OPT(,) 5) becomes DEF_OR_ARG( 5)
When SUM is called with a second argument, ... is this second argument and __VA_OPT__ expands to the value given which is a coma. In that case
DEF_OR_ARG(__VA_ARGS__ __VA_OPT(,) 5) becomes DEF_OR_ARG(second_argument_of_SUM , 5)
Now the expansion of DEF_OR_ARG happens. This one is easy since it considers only the first argument and just discards the rest.
So, when SUM was called with no second argument (first case above), the first argument to DEF_OR_ARG is our default value. In the case there was a second argument to SUM, it becomes the first argument to DEF_OR_ARG that will expand to that and discard the default which is now second argument.
Try this.
#include <stdio.h>
#include <stdarg.h>
/* print all non-negative args one at a time;
all args are assumed to be of int type */
void printargs(int arg1, ...)
{
va_list ap;
int i;
va_start(ap, arg1);
for (i = arg1; i >= 0; i = va_arg(ap, int))
printf("%d ", i);
va_end(ap);
putchar('\n');
}
int main(void)
{
printargs(5, 2, 14, 84, 97, 15, 24, 48, -1);
printargs(84, 51, -1);
printargs(-1);
printargs(1, -1);
return
0;
}
There is a way to support as many default parameters you need, just use a structure.
// Populate structure with var list and set any default values
struct FooVars {
int int_Var1 = 1; // One is the default value
char char_Command[2] = {"+"};
float float_Var2 = 10.5;
};
struct FooVars MainStruct;
//...
// Switch out any values needed, leave the rest alone
MainStruct.float_Var2 = 22.8;
Myfunc(MainStruct); // Call the function which at this point will add 1 to 22.8.
//...
void Myfunc( struct FooVars *MyFoo ) {
switch(MyFoo.char_Command) {
case '+':
printf("Result is %i %c %f.1 = %f\n" MyFoo.int_Var1, MyFoo.char_Command, MyFoo.float_Var2, (MyFoo.float_Var2 + MyFoo.int_Var1);
break;
case '*':
// Insert multiply here, you get the point...
break;
case '//':
// Insert divide here...
break;
}
}
As far as I know ANSI C doesn't directly support function overloading or default arguments. The standard substitute for overloading is adding suffixes to the function name indicating the argument types. For example, in OpenGL, a "3fv" suffix to a function name means the function takes a vector of three floats.
Default arguments can be viewed as a special case of function overloading.
Neither of default values or function overloading exists in ANSI C, so you'll have to solve it in a different way.
You can't so easily since C does not support them. The simpler way to get "fake overloading" is using suffixes as already said... default values could be simulated using variable arguments function, specifying the number of args passed in, and programmatically giving default to missing one, e.g.:
aType aFunction(int nargs, ...)
{
// "initialization" code and vars
switch(nargs)
{
case 0:
// all to default values... e.g.
aVar1 = 5; // ...
break;
case 1:
aVar1 = va_arg(arglist, int); //...
// initialize aVar2, 3, ... to defaults...
break;
// ...
}
}
Also overloading can be simulated using var args with extra informations to be added and passed and extracode... basically reproducing a minimalist object oriented runtime ...
Another solution (or indeed the same but with different approach) could be using tags: each argument is a pair argument type + argument (an union on the whole set of possible argument type), there's a special terminator tag (no need to specify how many args you're passing), and of course you always need "collaboration" from the function you're calling, i.e. it must contain extra code to parse the tags and choose the actual function to be done (it behaves like a sort of dispatcher)
You'll have to declare each C++ overloaded function differently in C because C doesn't do name mangling. In your case "foo_property1" "foo_property2".
i think u can use a function with variable arguments here is my example
#include <stdarg.h>
#include <stdio.h>
void baz( int flag, ... )
{
va_list ap;
char *bar = "baz"; /* default value */
va_start( ap, flag );
if ( flag == 1 )
bar = va_arg( ap, char * );
va_end( ap );
printf( "%s\n", bar );
}
int main( void )
{
baz( 0 );
baz( 1, "foo");
baz( 2 );
baz( 1, "bar");
return 0;
}
the output is
baz
foo
baz
bar
if u look for example man 2 open they say
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
int openat(int dirfd, const char *pathname, int flags);
int openat(int dirfd, const char *pathname, int flags, mode_t mode);
but mode is actually a ... argument
I was wondering if there was any way to pass parameters dynamically to variadic functions. i.e. If I have a function
int some_function (int a, int b, ...){/*blah*/}
and I am accepting a bunch of values from the user, I want some way of passing those values into the function:
some_function (a,b, val1,val2,...,valn)
I don't want to write different versions of all these functions, but I suspect there is no other option?
Variadic functions use a calling convention where the caller is responsible for popping the function parameters from the stack, so yes, it is possible to do this dynamically. It's not standardized in C, and normally would require some assembly to manually push the desired parameters, and invoke the variadic function correctly.
The cdecl calling convention requires that the arguments be pushed in the correct order, and after the call, the bytes pushed as arguments before the call are popped. In this way, the called function can receive an arbitrary number of parameters, as the caller will handle reverting the stack pointer to it's pre-call state. The space occupied by the arguments before the ... is the safe lower bound for number of bytes pushed. Additional variadic arguments are interpreted at runtime.
FFCALL is a library which provides wrappers for passing parameters dynamically to variadic functions. The group of functions you're interested in is avcall. Here's an example calling the functions you gave above:
#include <avcall.h>
av_alist argList;
int retVal;
av_start_int(argList, some_function, retval);
av_int(argList, a);
av_int(argList, b);
av_type(argList, val1);
...
av_type(argList, valn);
av_call(argList);
You might also find this link discussing generating wrappers around variadic functions in C, to be of interest in justifying why this isn't part of standard C.
A standard approach is to have each variadic function accompanied by a va_list-taking counterpart (as in printf and vprintf). The variadic version just converts ... to a va_list (using macros from stdarg.h) and calls its va_list-taking sister, which does actual work.
It might be interesting to try just passing an array, and then use the vararg macros anyway. Depending on stack alignment, it might Just Work (tm).
This is probably not an optimal solution, I mainly posted it because I found the idea interesting.
After trying it out, this approach worked on my linux x86, but not on x86-64 - it can probably be improved. This method will depend on stack alignment, struct alignment and probably more.
void varprint(int count, ...)
{
va_list ap;
int32_t i;
va_start(ap, count);
while(count-- ) {
i = va_arg(ap, int32_t);
printf("Argument: %d\n", i);
}
va_end(ap);
}
struct intstack
{
int32_t pos[99];
};
int main(int argc, char** argv)
{
struct intstack *args = malloc(sizeof(struct intstack));
args->pos[0] = 1;
args->pos[1] = 2;
args->pos[2] = 3;
args->pos[3] = 4;
args->pos[4] = 5;
varprint(5, *args);
return 0;
}
Depending on what it is you're passing around, it could be a discriminated union you're after here (as hinted at in the comments). That would avoid the need for variadic functions or arrays of void*, and answers the question "how does some_function know what you actually passed it". You might have code something like this:
enum thing_code { INTEGER, DOUBLE, LONG };
struct thing
{
enum thing_code code;
union
{
int a;
double b;
long c;
};
};
void some_function(size_t n_things, struct thing *things)
{
/* ... for each thing ... */
switch(things[i].code)
{
case INTEGER:
/* ... */
}
}
You can take this a step further and avoid the switch by replacing the code with one or more pointers to functions that do something useful with each thing. For example, if what you wanted to do was to simply print out each thing, you could have this:
struct thing
{
void (*print)(struct thing*);
union
{
...
};
}
void some_function(size_t n_things, struct thing *things)
{
/* .. for each thing .. */
things[i]->print(things[i]);
/* ... */
}