将许多参数传递给 C 函数 [英] pass many arguments to a C function

查看:25
本文介绍了将许多参数传递给 C 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将多个参数传递给 C 函数?假设我有这个功能:

How can I pass many arguments to a C function? Assuming that I have this function:

void f(int n, char* a, char* b, ...)

我想要一个未定义数量的 char* 参数.我该怎么做?

I want an undefined number of char* arguments. How can I do so?

推荐答案

你需要的是可变数量的参数函数,你可以阅读:9.9.可变数量的论点 一个很好的论文教程.

What you needs is called variable number of argument functions, you can read from : 9.9. Variable numbers of arguments a good and essay tutorial.

四点的简短理论将帮助您理解我的代码:

A short theory in four points will help you to understand my code:

  1. 必须包含 头文件,他引入了一个新的类型,称为 va_list,以及三个对对象进行操作的函数这种类型,称为 va_start、va_arg 和 va_end.
  2. va_start: 是一个宏,用于将 arg_ptr 设置为列表 ap 的开头可选参数
  3. va_arg: 就是使用这个保存的堆栈指针,并提取提供的类型的正确字节数
  4. va_end: 是一个用于重置ap的宏,在所有参数都已经检索到,va_end 将指针重置为 NULL.
  1. the <stdarg.h> header file must be included, his introduces a new type, called a va_list, and three functions that operate on objects of this type, called va_start, va_arg, and va_end.
  2. va_start: is a macro to set arg_ptr to beginning of list ap of optional arguments
  3. va_arg: does is use this saved stack pointer, and extract the correct amount of bytes for the type provided
  4. va_end: is a macro to reset ap, After all arguments have been retrieved, va_end resets the pointer to NULL.

这个理论还不够,下面的一个例子(根据你的需要)将帮助你理解基本的工作流程/和步骤:(每 4 个步骤阅读评论)

This theory is not enough but below an example (as you required) will help you to understand basic work-flow/ and steps: (read comment for each 4 steps)

//Step1: Need necessary header file
#include <stdarg.h>     
void f(int first, char* a, char* b, ...){   
    va_list ap;  // vlist variable
    int n;       // number 
    char aa,     
    int i;
    float f;
   //print fix numbers of arguments
    printf("\n %d, %s, %s\n", first, a, b);

   //Step2: To initialize `ap` using right-most argument that is `b` 
    va_start(ap, b); 

   //Step3: Now access vlist `ap`  elements using va_arg()
     n = va_arg(ap, int); //first value in my list gives number of ele in list 
     while(n--){
       aa = (char)va_arg(ap, int); // notice type, and typecast
       i = va_arg(ap, int);
       f = (float)va_arg(ap, double);   
       printf("\n %c %d %f \n", aa,i, f);
    }

    //Step4: Now work done, we should reset pointer to NULL
    va_end(ap); 
}
int main(){
    char* a = "Aoues";
    char* b = "Guesmi";
    f(2, a, b, 3, 'a', 3, 6.7f, 'b', 5, 5.5f, 'A', 0, 0.1);
    //         ^ this is `n` like count in variable list
    return 1;
}

谁运行:

~$ ./a.out 
 2, Aoues, Guesmi
 a 3 6.700000 
 b 5 5.500000 
 A 0 0.100000 

对我的代码的简要说明将对未来的用户有所帮助:

A brief explanation of my code will be helpful for future users:

  1. 实际上函数是固定数量的参数后跟变量参数的数量.和最右边的函数参数(在固定在我们的函数 f()) 中是 char* b 的参数列表仅用于初始化可行列表ap.
  2. 第一个函数f()读取n值为3(读取主要评论).在f()中,while(n--)执行了三个时间和每次循环使用 va_arg() 宏我们检索三个值.
  3. 如果你注意到我读了前两个 ints 然后是一个 double,而我正在发送 char、int、float(在 main 中我调用 f() 的通知).这是因为在变量参数列表的情况下自动类型提升.(从上面的链接中详细阅读)
  1. Actually function is fixed number of arguments followed by variable number of arguments. And right-most argument to function (in fixed argument list that is char* b in our function f()) uses just to initialized viable list ap.
  2. The function f() above fist reads n value that is 3 (read comment in main). In f(), while(n--) executes for three time and each time in loop using va_arg() macro we retrieves three values.
  3. If you notice I reads first two ints then a double, Where as I am sending char, int, float (notice in main where I call f()). this is because auto type promote in case of variable argument list. (read in detail from above lisk)

她是来自 MSDN 的另一个有用的链接:va_arg、va_end、va_start.

Her is one more useful link from MSDN: va_arg, va_end, va_start.

(如果您需要更多帮助,请告诉我)

这篇关于将许多参数传递给 C 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆