C的变参数函数 variable arguments in C

 

今天在csdn上看到这样的例子,是用来给一个数组赋值的,是用了va_list。一直没有很好的关心过这个变参数函数的使用及细节,以前都是用int argc ,char *argv[] 来完成这些工作的。

把那个星星先生的代码贴出来,我做了少许修改:

#include <stdio.h>
#include "stdarg.h"


void array_set(char* parray, ...)
{
va_list va;
int n = 0;
char c = 0;
va_start(va, parray);
while (1)
{
c = va_arg(va, char);
if (c != 0)
{
parray[n++] = c;
}
else
{
break;
}
}

va_end(va);

}

//这个是我来测试用的实例代码,注意array_set最后的参数0,我还需要修改一下array_set。

//至少不该手动加结束符。

int main()
{
char arr[10];
array_set(arr,48,49,49,49,49,49,49,0); //这里需要一个0来终止字符串
printf("%s/n",arr);
getchar();
}

 

下面是一些参考文献:

The <stdarg.h> Header File stdarg中的信息:
--------------------------------------------------------------------------------

ANSI routines for creating functions with variable numbers of arguments

Functions
va_arg
Returns the current argument in the list.
va_end
va_end helps the called function perform a normal return.
va_start
Initializes a pointer to a variable argument list.
Predefined Types
va_list
A void pointer which can be interpreted as an argument list.

--------------------------------------------------------------------------------

va_arg
type va_arg (va_list &ap, type);


Returns the current argument in the list.

This function (also implemented as a macro) expands to an expression that has the same type and value as the next argument being passed (one of the variable arguments). The variable ap to va_arg should be the same ap that va_start initialized. Because of default promotions, you can't use char or unsigned char types with va_arg.

The first time va_arg is used, it returns the first argument in the list. Each successive time va_arg is used, it returns the next argument in the list. It does this by first dereferencing ap, and then incrementing ap to point to the following item.

va_arg uses the parameter type (which must be an expected type name) to both perform the dereference and to locate the following item. Each successive time va_arg is invoked, it modifies ap to point to the next argument in the list.


--------------------------------------------------------------------------------

va_end
void va_end (va_list &ap);


va_end helps the called function perform a normal return.

va_end might modify ap in such a way that it cannot be used unless va_start is recalled. va_end should be called after va_arg has read all the arguments.

Note: va_end is introduced here only to increase compatibility with ANSI C. In this implementation, va_end in fact does nothing.


--------------------------------------------------------------------------------

va_start
void va_start (va_list &ap, &lastfix);


Initializes a pointer to a variable argument list.

Some C functions, such as sprintf have a variable number of arguments. This function, together with va_arg and va_end, provides a portable way to access these argument lists. They are used for stepping through a list of arguments when the called function does not know the number and types of the arguments being passed. Function va_start (implemented as a macro) sets ap to point to the first of the variable arguments being passed to the function. It must be used before the first call to va_arg or va_end.

va_start takes two parameters: ap and lastfix. ap is explained above, and lastfix is the name of the last fixed parameter being passed to the called function.

Note: I used notation "&ap" in the prototype description, although passing by reference does not exist in ordinary C (only in C++). However, this macro is implemented in such a way that it simulates "passing by reference".


--------------------------------------------------------------------------------

va_list
typedef void *va_list;


A void pointer which can be interpreted as an argument list.

va_list is the type of the void pointer passed to one of the functions that accepts a pointer to a list of arguments. This array holds information needed by va_arg and va_end.

gcc  library 中的专题:

http://www.delorie.com/gnu/docs/glibc/libc_670.html

一些其他链接:

http://www.yourblog.org/Data/20041/2523.html

http://blog.chinaunix.net/index.php?blogId=97

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值