简体   繁体   English

在纯 C 中,如何将可变数量的参数传递给函数?

[英]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_startva_argva_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

It sounds like you are looking for varargs .听起来您正在寻找varargs

#include <stdarg.h>
void foo(const char *fmt, ...)
{
  va_list argp;
  va_start(argp, fmt);
  int i = va_arg(argp, int);
  // Do stuff...
  va_end(argp);
}

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:我将按照从最不通用到最多的顺序列出它们:

  1. Create multiple overloads of the same function.创建同一函数的多个重载。 As I recall, you cannot do this in C.我记得,你不能在 C 中做到这一点。

  2. Use variadic functions.使用可变参数函数。 Just Google this: http://www.google.com/search?q=variadic+function+c只是谷歌这个: http : //www.google.com/search?q=variadic+function+c

  3. 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.

相关问题 我如何将数组作为args列表传递给C中具有可变数量参数的函数 - How can I pass an array as a list of args to a function with varying number of parameters in C 如何通过可变数量的参数传递函数中的参数列表? - How to pass list of arguments in function with variable number of parameters? 如何将const数组或变量数组传递给C中的函数? - How can I pass a const array or a variable array to a function in C? 如何在C中调用(不定义)具有可变数量参数的函数? - How can I call (not define) a function with a variable number of arguments in C? 如何在不限制参数数量的函数中传递参数? - How to pass parameters in function that not limit the number of parameters? 我似乎无法弄清楚如何使用纯指针符号正确传递参数 - I can't seem to figure out how to properly pass parameters with pure pointer notation 如何将变量传递给函数? - How can I pass a variable to a function here? 如何使用可变数量的参数调用函数? - How do I call a function with a variable number of parameters? C ++中函数的可变参数数量 - Variable number of parameters in function in C++ C中参数数量可变的函数的奇数行为 - Odd behavior of function with variable number of parameters in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM