Variable parameter Programming in the "reprint" C language

Source: Internet
Author: User

The most common use of variable-length parameters in the C language is the following two functions:

int printf (const char *format, ...); int scanf (const char *format, ...);

So how do they implement support into parameters? The implementation part of the function that uses the variable-length parameter (which is assumed to be func) actually uses several macros inside the stdarg.h to access the indeterminate parameters, respectively:

void Va_start (Va_list ap, last); type Va_arg (va_list ap, type); void Va_end (va_list ap);

Suppose Lastarg is the last named parameter of Func, that is, in the Func function definition ... The previous parameter (Lastarg is format in printf), first defines a variable in func:

Va_list AP

This variable will then point to each variable parameter in turn. The AP must be initialized once with the macro va_start before it is used, as follows:

Va_start (AP, Lastarg);

Where Lastarg is the last named parameter in Func. You can then use Va_arg to get the next indeterminate parameter (provided that you know the type of the indeterminate parameter):

Type next = Va_arg (AP, type)

The last is to use the macro va_end to clean the scene.

Let's implement a variable parameter function by ourselves:

1 #include <stdio.h> 2 #include <stdarg.h> 3  4 void func (char *fmt, ...) 5 {6     va_list ap; 7  8
   
    va_start (AP, FMT); 9     while (*FMT) one     {*fmt)-{+ case             ' d ':                 fprintf (stdout, "%d\n", (int) va_arg (AP, int));                 break;17 case             ' C ':                 fprintf (stdout, "%c\n", (char) Va_arg (AP, int.)),                 break;20 case             ' s ':                 fprintf (stdout, "%s\n", (char *) Va_arg (AP, char *)); break;23 default:24                 fprintf (stderr, "Error fmt\n");         }26         fmt ++;27     }28     va_end (AP);}30 int main (int argc, char *argv[]) [33
    func ("DCs", ten, ' s ', "Hello");     0;35}/               *----------  end of function main  ----------*/
   

Output Result:

10shello

You can see that the above program completes the access to the variable-length parameter as we wish, and controls the type of the next indeterminate parameter through the previous FMT. So how do these three macros achieve access to indeterminate parameters? Let's see how they are implemented:

Va_list is actually a pointer to each indeterminate parameter, because the type of the parameter is indeterminate, you can define va_list as void * or char * type, i.e.

#define VA_LIST void *

Va_start is the position where va_list points to the last named parameter lastarg the function, which is the first indeterminate argument.

#define VA_START (AP, Lastarg) (AP = (va_list) &lastarg + sizeof (LASTARG))

Va_arg gets the value of the current indeterminate parameter, moving the pointer to the next indeterminate parameter based on the size of the current parameter's type.

#define VA_ARG (AP, type) (* (type *) (AP + = sizeof (type))-sizeof (type))

Va_end clear the pointer 0.

#define VA_END (AP) (ap= (va_list) 0)

In essence, it is by the front lastarg to control the type of indefinite parameter, va_list variable to point to the address of the indefinite parameter, then obtain the indefinite parameter according to Lastarg one.

Variable parameter Programming in the "reprint" C language

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.