通过大量的参数传递给C函数 [英] pass many arguments to a C function
问题描述
我如何传递多个参数到C函数?
假设我有这个功能:
无效F(INT N,字符*一,字符* B,...)
我想要的char *参数未定义的数量。我该怎么办呢?
您需要什么叫可变数量的参数功能,可以读出:的 9.9。可变的参数个数的一个很好的和作文教程。
:四点会帮助你了解我的code A 短论 这理论是不够的,但下面的例子(如您需要)将帮助你了解基本的工作流程/和步骤:(阅读评论为每4个步骤的)<&STDARG.H GT;
头文件中必须包含,他引入了一个新的
该对象上的操作的类型,称为的va_list,和三个功能
这种类型,叫做的va_start,在va_arg,va_end用来
。的va_start:
是一个宏设置 arg_ptr
来列表的开头 AP
的
可选的参数的va_arg:
确实是使用这个存储堆栈指针,并提取
正确的金额所提供的类型字节 va_end用来:
是重置宏 AP
后,所有的参数都被
检索, va_end用来
指针重置为NULL。
//第一步:需要必要的头文件
#包括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, calledva_start, va_arg, and va_end
. va_start:
is a macro to setarg_ptr
to beginning of listap
of optional argumentsva_arg:
does is use this saved stack pointer, and extract the correct amount of bytes for the type providedva_end:
is a macro to resetap
, 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 functionf()
) uses just to initialized viable listap
. - The function
f()
above fist readsn
value that is3
(read comment in main). Inf()
,while(n--)
executes for three time and each time in loop usingva_arg()
macro we retrieves three values. - If you notice I reads first two
ints
then adouble
, Where as I am sendingchar, 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函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!