C的變參數函數 variable arguments in C

來源:互聯網
上載者:User
 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.