本文介绍如何使用 C 语言标准库 <stdarg.h> 中的宏来处理函数中的可变数量参数。主要内容包括 va_start、va_arg 和 va_end 宏的用法,以及一个字符串拼接的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#define va_arg(va_list ap, T) <rvalue of type T>
#define va_end(va_list ap) <void expression></I>
typedef do-type va_list;
#define va_start(va_list ap, last-par) <void expression>
 

Include the standard header <stdarg.h> to access the unnamed additional arguments (arguments with no corresponding parameter declarations) in a function that accepts a varying number of arguments. To access the additional arguments:

  • The program must first execute the macro va_start within the body of the function to initialize an object with context information.
  • Subsequent execution of the macro va_arg, designating the same context information, yields the values of the additional arguments in order, beginning with the first unnamed argument. You can execute the macro va_arg from any function that can access the context information saved by the macro va_start.
  • If you have executed the macro va_start in a function, you must execute the macro va_end in the same function, designating the same context information, before the function returns.

You can repeat this sequence (as needed) to access the arguments as often as you want.

You declare an object of type va_list to store context information. va_list can be an array type, which affects how the program shares context information with functions that it calls. (The address of the first element of an array is passed, rather than the object itself.)

For example, here is a function that concatenates an arbitrary number of strings onto the end of an existing string (assuming that the existing string is stored in an object large enough to hold the resulting string):

#include <stdarg.h>
void va_cat(char *s, ...)
    {
    char *t;
    va_list ap;

    va_start(ap, s);
    while (t = va_arg(ap, char *)) null pointer ends list
        {
        s += strlen(s);            skip to end
        strcpy(s, t);              and copy a string
        }
    va_end(ap);
    }

va_arg

#define va_arg(va_list ap, T) <rvalue of type T>

The macro yields the value of the next argument in order, specified by the context information designated by ap. The additional argument must be of object type T after applying the rules for promoting arguments in the absence of a function prototype.

va_end

#define va_end(va_list ap) <void expression></I>

The macro performs any cleanup necessary, after processing the context information designated by ap, so that the function can return.

va_list

typedef do-type va_list;

The type is the object type do-type that you declare to hold the context information initialized by va_start and used by va_arg to access additional unnamed arguments.

va_start

#define va_start(va_list ap, last-par) <void expression>

The macro stores initial context information in the object designated by ap. last-par is the name of the last parameter you declare. For example, last-par is b for the function declared as int f(int a, int b, ...). The last parameter must not have register storage class, and it must have a type that is not changed by the translator. It cannot have:

  • an array type
  • a function type
  • type float
  • any integer type that changes when promoted

See also the Table of Contents and the Index.

Copyright © 1989-1996 by P.J. Plauger and Jim Brodie. All rights reserved.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值