[英]In pure C how can I pass a variable number of parameters into a function?
How can i pass (and access) using C, not c++, variable parameters into a function?我如何使用 C 而不是 C++ 将可变参数传递(和访问)到函数中?
void foo(char* mandatory_param, char* optional_param, char* optional_param2...)
thanks谢谢
/fmsf /fmsf
Use stdarg.h
使用
stdarg.h
You need to use va_list
and then use the macros va_start
, va_arg
, and va_end
.您需要使用
va_list
,然后使用宏va_start
、 va_arg
和va_end
。
For more information, see http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.10.html有关更多信息,请参阅http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.10.html
阅读C 中的变量参数
In a language that does not support optional parameters directly, there are a few ways to achieve a similar effect.在不直接支持可选参数的语言中,有几种方法可以实现类似的效果。 I will list them in order from the least versatile to the most:
我将按照从最不通用到最多的顺序列出它们:
Create multiple overloads of the same function.创建同一函数的多个重载。 As I recall, you cannot do this in C.
我记得,你不能在 C 中做到这一点。
Use variadic functions.使用可变参数函数。 Just Google this: http://www.google.com/search?q=variadic+function+c
只是谷歌这个: http : //www.google.com/search?q=variadic+function+c
I recommend this: Create a "params" or "args" class (or struct in C), like this:我建议这样做:创建一个“params”或“args”类(或 C 中的结构),如下所示:
) )
// untested C code
struct FooArgs {
char * mandatory_param;
char * optional_param;
char * optional_param2;
// add other params here;
};
and then make your method call take in a single argument:然后让你的方法调用接受一个参数:
// untested
void foo(struct fooArgs * args)
This way, as needs change, you can add parameters to fooArgs without breaking anything.这样,随着需求的变化,您可以在不破坏任何内容的情况下向 fooArgs添加参数。
#include <stdarg.h>
void do_sth (int foo, ...)
{
int baz = 7; /* "baz" argument */
const char *xyz = "xyz"; /* "xyz" argument */
/* Parse named parameters */
va_list ap;
va_start (ap, foo);
for (;;) {
const char *key = va_arg (ap, char *);
if (key == NULL) {
/* Terminator */
break;
} else if (strcmp (key, "baz") == 0) {
baz = va_arg (ap, int);
} else if (strcmp (key, "xyz") == 0) {
xyz = va_arg (ap, char *);
} else {
/* Handle error */
}
}
va_end (ap);
/* do something useful */
}
do_sth (1, NULL); // no named parameters
do_sth (2, "baz", 12, NULL); // baz = 12
do_sth (3, "xyz", "foobaz", NULL); // xyz = "foobaz"
do_sth (4, "baz", 12, "xyz", "foobaz", NULL); // baz = 12, xyz = "foobaz"
Variadic functions and arguments assignment in C/C++ C/C++ 中的可变参数函数和参数赋值
I have a solution that does not use VA_LIST in pure C. However, it works at 32bits only.我有一个在纯 C 中不使用 VA_LIST 的解决方案。但是,它仅适用于 32 位。 Here, what happens is that each parameter of the call stack occupies as many bytes according to its type.
在这里,调用堆栈的每个参数根据其类型占用尽可能多的字节。 It is possible to create a structure with a size larger than 4 or 8 bytes, so just align all the parameters in this structure.
可以创建大小大于 4 或 8 个字节的结构,因此只需对齐该结构中的所有参数即可。
int printf(void*,...);
typedef struct{
char p[1024];
}P_CALL;
int soma(int a,int b){
return a+b;
}
void main(){
P_CALL
call;
char
*pcall=(void*)&call;
int
(*f)()=soma,
res;
*(int*)pcall=1;
pcall+=sizeof(void*);
*(int*)pcall=2;
pcall+=sizeof(void*);
res=f(call);
printf("%d\n",res);//3
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.