通过大量的参数传递给C函数

编程入门 行业动态 更新时间:2024-10-12 16:26:34
本文介绍了通过大量的参数传递给C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

无效F(INT N,字符*一,字符* B,...)

我想要的char *参数未定义的数量。我该怎么办呢?

解决方案

您需要什么叫可变数量的参数功能,可以读出:的 9.9。可变的参数个数的一个很好的和作文教程。

:四点会帮助你了解我的code

A 短论

  • 在<&STDARG.H GT; 头文件中必须包含,他引入了一个新的该对象上的操作的类型,称为的va_list,和三个功能这种类型,叫做的va_start,在va_arg,va_end用来。
  • 的va_start:是一个宏设置 arg_ptr 来列表的开头 AP 的可选的参数
  • 的va_arg:确实是使用这个存储堆栈指针,并提取正确的金额所提供的类型字节
  • va_end用来:是重置宏 AP 后,所有的参数都被检索, va_end用来指针重置为NULL。
  • 这理论是不够的,但下面的例子(如您需要)将帮助你了解基本的工作流程/和步骤:(阅读评论为每4个步骤的)

    //第一步:需要必要的头文件#包括LT&;&STDARG.H GT;无效F(第一INT,字符*一,字符* B,...){    va_list的AP; //变量VLIST    INT N; //号    字符AA,    INT I;    浮F;   //参数打印固定号码    的printf(\\ N%D,%S,%S \\ n,第一,A,B);   //第二步:初始化`ap`使用最右边的参数为`B`    的va_start(AP,B);   //第三步:现在访问VLIST`使用的va_arg ap`元素()     N =在va_arg(AP,INT); //我的列表中第一个值给出列表ELE数     而(N - ){       AA =(char)的的va_arg(AP,INT); //通知类型,类型转换       我的va_arg =(AP,INT);       F =(浮动)的va_arg(AP,双面);       的printf(\\ n%C%d个%F \\ N,AA,I,F);    }    //第四步:现在的工作完成后,我们应该重新设置指针为NULL    va_end用来(AP);}诠释主(){    字符* A =Aoues;    字符* B =Guesmi;    F(2,A,B,3,'一个',3,6.7f,'B',5,5.5F,A,0,0.1);    // ^这是`变量名单N`像数    返回1;}

    谁来做它运行:

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

    一个我的code的简要说明将有利于未来的用户:

    其次是变量
  • 的参数其实功能是定数参数的数目。而最右边的参数功能(固定即的char * B 在我们的函数 F())使用只是为了参数列表初始化列表可行 AP 。
  • 函数 F()上面的拳头读 N 值, 3 (阅读在主要的意见的)。在 F(),而(N - )执行三时间和使用在va_arg()宏每一次循环中,我们检索出三值。
  • 如果你注意到我第一次读取两个整数则双击,其中我我发送字符,整型,浮点(主通知,我叫F()的)。这是因为汽车类型变量参​​数列表的情况下,推广。(从上面lisk详细阅读)
  • 她是从MSDN多了一个有用的链接:在va_arg中,va_end,va_start中。

    (让我知道如果你需要对更多的帮助这个的)

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

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

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

    解决方案

    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:

  • 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.
  • va_start: is a macro to set arg_ptr to beginning of list ap of optional arguments
  • va_arg: does is use this saved stack pointer, and extract the correct amount of bytes for the type provided
  • va_end: is a macro to reset ap, After all arguments have been retrieved, va_end resets the pointer to NULL.
  • 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; }

    Who does it runs:

    ~$ ./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:

  • 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.
  • 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.
  • 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)
  • Her is one more useful link from MSDN: va_arg, va_end, va_start.

    (let me know if you need more help regarding this)

    更多推荐

    通过大量的参数传递给C函数

    本文发布于:2023-11-24 02:21:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1623617.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:函数   参数

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!