Variable Parameter Function Variable Arguments in C

Source: Internet
Author: User

Today, we can see in csdn that this example is used to assign values to an array and va_list is used. I have never been very concerned about the use and details of this variable parameter function. In the past, I used int argc and char * argv [] to do this.

I posted the code of Mr. Xing and made a few changes:

# 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 );

}

// This is the instance code for testing. Note that the final parameter 0 of array_set is required.

// At least the terminator should not be manually added.

Int main ()
{
Char arr [10];
Array_set (ARR, 48, 49, 49,49, 49,49, 49,0); // a 0 string is required to terminate the string.
Printf ("% s/n", arr );
Getchar ();
}

 

Below are some references:

The information in 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 shoshould 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 shocould 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 funents that accepts a pointer to a list of arguments. This array holds information needed by va_arg and va_end.

Topics in GCC Library:

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

Other links:

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

Http://blog.chinaunix.net/index.php? Blogid = 97

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.