Is there a nicer way to do c named arguments?
I'm trying to implement a flexible debug macro/function lib and named/optional arguments seem like the best way to implement the functions.
Is there a nicer way to do named arguments in c then the following?
enum named_args {NAME,ADRESS,AGE,NA_SENTINEL};
void named_arg_initializers(struct person p, enum * named_args)
{
enum named_args * current_name;
enum named_args * current_arg;
...
if(named_args==NULL)
return;
current_name = named_args[0];
current_arg = named_args[1];
while(current_name!=NA_SENTINEL)
{
current_name+=2;
current_arg+=2;
if(current_name==NAME)
开发者_如何学运维 p.name=current_arg;
else if(...
...
}
...
}
...
}
Sure.
struct toto {
unsigned age;
char name[25];
};
void func(struct toto);
...
func((struct toto){ .name = "you", .age = 18 });
or if you want you may wrap that in a macro
#define FUNC(...) func((struct toto){ __VA_ARGS__ })
...
FUNC(.name = "you", .age = 18 );
The way you've shown isn't valid unless the named arguments are all compatible with enum
(you could fix that by using a void *
argument).
However, you could do something similar with varargs which looks neater:
#include <stdarg.h>
enum named_args { NAME, ADDRESS, AGE, NA_SENTINEL };
void named_arg_initializers(struct person *p, ...)
{
va_list ap;
enum named_args argn;
va_start(ap, p);
for (argn = va_arg(ap, enum named_args); argn != NA_SENTINEL; argn = va_arg(ap, enum named_args))
{
switch (argn)
{
case NAME:
p->name = va_arg(ap, char *);
break;
case AGE:
p->age = va_arg(ap, int);
break;
/* ... */
}
}
va_end(ap);
/* ... */
}
You'd use it like so:
named_arg_initializers(&p, AGE, 110, NAME, "Claude Choules", NA_SENTINEL);
https://github.com/cindRoberta/C/blob/master/structure/function/named_parameter.c
#include<stdio.h>
typedef struct {
int a;
float b;
} named_param;
void f_impl(named_param np) {
printf("%d %g", np.a, np.b);
}
#define f_macro(...) f_impl((named_param){__VA_ARGS__})
int main() {
f_macro(.a = 4, .b = 9.7);
//f_impl((named_param){.a = 7, .b = 6.2});
return 0;
}
精彩评论