分享
 
 
 

C的变参数函数 variable arguments in C

王朝other·作者佚名  2006-02-01
窄屏简体版  字體: |||超大  

今天在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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有